• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeMap question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic