File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeMap question " Watch "TreeMap question " New topic

TreeMap question

john kidwell

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:


These states had no presidents were born in them:


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: 462
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:


Just give it a thought.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38107
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

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

Joined: Oct 13, 2005
Posts: 38107
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.
jQuery in Action, 2nd edition
subject: TreeMap question
Similar Threads
How to show text in textarea on event
Big Kahoonass
Ronald Reagan dead at 93
dependent select list options
JOptionPane and Arrays