This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a map to store values from a scanned file  RSS feed

 
Tammi Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get help on the first part of my assignment.
The assignment deals with popular baby names (girls/boys)

So we are to use scanner to read a txt file...
A sample line from .txt "7 William 421,592 Linda 225,412".
We are encouraged to store in a Map<String, Object> with string being the name of the girl/boy. We are suppose to have a map for girls and one for boys.

Basically, I want...
boysMap
key(String) = "William"
Object = 7 421,592

girlsMap
key(String) = "Linda"
Object = 7 225,412

1st issue is that I can't even get past creating the map.
My version that isn't working "Map<String, BoysInfo> boys = new HashMap<>();" --> Java is saying it cannot find symbol for "BoysInfo".
2nd issue: How to scan the line in the way I would like it to be stored in the map?

I attached a few items for someone to better understand my assignment.

I greatly appreciated any help!:-)
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got a class called BoysInfo?
 
Tammi Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't have a class called BoysInfo. Do I need to put "Object" there instead of BoysInfo?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no. What is "7 421,592" supposed to be?

And Welcome to the Ranch!
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want three classes: ChildInfo BoyInfo and GirlInfo. You should make two subclasses of the other. You can probably do that by writing a constructor and nothing else in the two subclasses.

And welcome again
 
Tammi Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I hope to learn a lot outside of the classroom setting....

I kind of figured out how to break up the scanned string(not sure if it was the best way) but I don't know how to pull just part of the object that was stored in the map.
For instance, I stored "William" as the key and " 7 421,592" as the object. "7" is the rank for the name William & "421,592" is the number of births associated with William.
Well I would need to pull just the two numbers separately...how would I accomplish that?



----
Now this is the method where I would have to pull just the rank associated with the name from the map...


 
Tammi Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is part of the txt to be scanned in and separated...

1 Michael 833,346 Lisa 496,948
2 David 734,113 Mary 355,195
3 John 713,581 Susan 287,637
4 James 684,950 Karen 286,028
5 Robert 650,935 Kimberly 259,078
6 Mark 441,519 Patricia 231,109
7 William 421,592 Linda 225,412


Each line in the text files contains five entries:
• The rank (from 1 to 200)
• The name and frequency of the male name of that rank
• The name and frequency of the female name of that rank

For example, the line below from the babynames60s.txt file
7 William 421,592 Linda 225,412
shows that the 7th most common boy’s name was Joseph, with 421,592 births during that period.
The 7th most common girl’s name was Linda.
Your assignment is to write a BabyNames class to build and process name information.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what have you got for a baby name class?
By the way, did you really say Joseph was on the 7th line?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tammi Armstrong wrote:For instance, I stored "William" as the key and " 7 421,592" as the object. "7" is the rank for the name William & "421,592" is the number of births associated with William.

OK, but you might want to read the StringsAreBad article before you go any further.

It's true that you have a lot of strings to deal with; but you need to understand what they represent.

It's fairly obvious that "William" is just a name, but what about "7 421,592"? It would seem to be some sort of ranking information, but maybe you can come up with a better name.
It also appears to be the same for both boys and girls, so you can probably use the same object to hold it for both,

So you might come up with something likeand then you could use a Map<String, NameInfo>. And this is what we mean by creating classes to meet your needs.

For a problem this simple you could possibly get away with just using strings, but IMO it's a bad habit to get into, because you'll soon run into problems where Strings simply aren't good enough.

Read the article above, and see if what I said makes any more sense.

HIH

Winston

PS: If you're only worried about converting a String to an int (or an Integer), have a look at Integer.parseInt(String) or Integer.valueOf(String).
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . but what about "7 421,592"? . . .
Agree about Strings being bad there. "7 421,592" is not a datum at all. It is two data.
You can of course get the information from the file withYou may have to do some fiddling to deal with the commas as thousand separators.

You can add a name field to the NameInfo class. And suitable getXXX methods. Then you can get the name back from the NameInfo object (getName) and use that as the “K” in a Map.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!