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...
key(String) = "William"
Object = 7 421,592
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!:-)
And welcome again
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...
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.
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.
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).
Agree about Strings being bad there. "7 421,592" is not a datum at all. It is two data.
Winston Gutkowski wrote:. . . but what about "7 421,592"? . . .
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.