wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes TreeMap question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeMap question " Watch "TreeMap question " New topic
Author

TreeMap question

john kidwell
Greenhorn

Joined: Nov 09, 2013
Posts: 6
Hey, So in my prompt output is supposed to be


The following states had these presidents born in them:

Arkansas Clinton
California Nixon
Connecticut Bush_GW
Georgia Carter
Hawaii Obama
Illinois Reagan
Iowa Hoover
Kentucky Lincoln
Massachusetts Bush_GHW Kennedy
Missouri Truman
Nebraska Ford
New_Hampshire Pierce
New_Jersey Cleveland
New_York Fillmore Roosevelt_F Roosevelt_T VanBuren
North_Carolina Johnson_A Polk
Ohio Garfield Grant Harding Harrison_B Hayes McKinley Taft
Pennsylvania Buchanan
Texas Eisenhower Johnson_L
Vermont Arthur Coolidge
Virginia Taylor Tyler Wilson

List of presidents and the state each was born in:

Arthur Vermont
Buchanan Pennsylvania
Bush_GHW Massachusetts
Bush_GW Connecticut
Carter Georgia
Cleveland New_Jersey
Clinton Arkansas
Coolidge Vermont
Eisenhower Texas
Fillmore New_York
Ford Nebraska
Garfield Ohio
Grant Ohio
Harding Ohio
Harrison_B Ohio
Hayes Ohio
Hoover Iowa
Johnson_A North_Carolina
Johnson_L Texas
Kennedy Massachusetts
Lincoln Kentucky
McKinley Ohio
Nixon California
Obama Hawaii
Pierce New_Hampshire
Polk North_Carolina
Reagan Illinois
Roosevelt_F New_York
Roosevelt_T New_York
Taft Ohio
Taylor Virginia
Truman Missouri
Tyler Virginia
VanBuren New_York
Wilson Virginia

These presidents were born before the states were formed:

Adams_J
Adams_JQ
Harrison_W
Jackson
Jefferson
Madison
Monroe
Washington

These states had no presidents were born in them:

Alabama
Alaska
Arizona
Colorado
Delaware
Florida
Idaho
Indiana
Kansas
Louisiana
Maine
Maryland
Michigan
Minnesota
Mississippi
Montana
Nevada
New_Mexico
North_Dakota
Oklahoma
Oregon
Rhode_Island
South_Carolina
South_Dakota
Tennessee
Utah
Washington
West_Virginia
Wisconsin
Wyoming


and I'm hardcoding my code with this text file :

Virginia Tyler Taylor Wilson
Ohio Grant Hayes Garfield Harrison_B McKinley Taft Harding
Massachusetts Kennedy Bush_GHW
New_York VanBuren Fillmore Roosevelt_T Roosevelt_F
North_Carolina Polk Johnson_A
Texas Eisenhower Johnson_L
Vermont Arthur Coolidge
Arkansas Clinton
California Nixon
Connecticut Bush_GW
Georgia Carter
Hawaii Obama
Illinois Reagan
Iowa Hoover
Kentucky Lincoln
Missouri Truman
Nebraska Ford
New_Hampshire Pierce
New_Jersey Cleveland
Pennsylvania Buchanan


I've been able to get the first two outputs correctly, though I can't get the second or third one. The third output is asking me to
"Print a sorted list of presidents who have no birth state associated with them. These presidents were actually born on this continent but were born before the states were created."

and the fourth prompt asks
Print a sorted list of states that have no president born in them. You may use the allstates.txt file for this.


Here's my code:


Does anyone know how I can go about trying to solve this problem?
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 533
    
    8
hi John,

well, in the same fashion: input a hard-coded list of states_without_presidents and another one: presidents_without_a_state.

But I don't think that your solution is what the originator of that question had in mind.

A nicer way would be to have a list of states, and a list of, say, President, where President is
a class like"


And I bet that the solutions involve shuffling your two lists with list-commands like:



and



Just give it a thought.

Greetings,
Piet
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
I have had to break the long lines in that post; you can see how it should be done. Please don't use code tags for what isn't code.

I am afraid you aren't going to like my answer, which means turning your computer off and writing what you plan to do on paper. When you know what to do, you will probably find it easy to turn into code.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 172
You need to iterate through your map of states and your map of presidents and check if the corresponding value is null, meaning that a state has no presidents or that a president does not have a state. If it is, put that key into a separate corresponding array. If not just move on. Then just sort your arrays and print.
john kidwell
Greenhorn

Joined: Nov 09, 2013
Posts: 6
I tried doing this and I know I'm doing this wrong but am I going in the right direction?



And also thank you campbell for editing my post. I'm kind of new to the website.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You're welcome

I am afraid that looks like the sort of code you get when you guess. You can guess a million times and probably get something which works, as long as all your guesses are different. Or you can design the code properly and it will work first time.

I suggest you stop coding and get a pencil paper and eraser (the last is by far the most important piece of hardware ‍). Write down how you would order your data by hand. Then work out how you are going to get the data back by state. When you have worked it out, then work out which data structure you can use (there are several sorts ready‑made in the collections framework). When you have those concepts worked out, then it sill be easy to convert the ideas to code.
Also work out what to do about somebody like Washington who appears not to have been born.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeMap question