• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading two files into seperate ArrayLists.  RSS feed

 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was wondering if anyone has any insight with the problem I'm having with the following question...

Write a Java application that reads two files into separate ArrayLists. Once you have read and stored the names prompt the user to enter in a boy’s name and a girl’s name. Check to see if the name is in the ArrayList and notify the user accordingly. Assuming the files are sorted from most popular to least popular also display the “rank” of the name if it is found. (Hint: remember the indices of an array are off by one). You can do all of this work in the main method. You can hardcode the names of the files.

A more elegant solution would be to use methods to break up these tasks. Implement the following:
• readNameFile: takes a string (the name of the file) as an argument and returns an
ArrayList with 200 names. Note: all file I/O is done inside of this method.
• searchForName: takes an ArrayList and a String as arguments. Returns the index of the name if it is in the ArrayList, -1 if it is not. Use linear search to accomplish this task.
The following is an example of the output...
java Names
Please enter one girl and one boy name separated by a new line:
Suzie
James

Suzie was not found in the list of 200 most popular girl names.
James was found, and was ranked 17 by popularity.

(Bold indicates user input.)


The following is what I have for the code:


The problem I am encountering is when I run the program the boyIndex doesn't find the rank properly, for example, in the above output name example of "James" ranking in 17 of popularity, when I run my code it ranks "James" at 217 in popularity (there's only 200 names in the file). The girls name has no issues, and the boyIndex is written the same way, I'm not sure where the problem is.

Anyone one have any advice?

Thank you in advance.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Karen,

the title of this topic is:

Reading two files into seperate ArrayLists.

Are you really using separate ArrayLists?
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the title of this topic is:

Reading two files into seperate ArrayLists.

Are you really using separate ArrayLists?


I thought I was, from your question, I'm assuming I am not?!

Computer programming is completely out of my realm, I just have to get passed this one question, and then I should be home free!!

That being said, I'm defintely not changing my code, all I'd like to know now is how to correct the output of the boyIndex to rank the proper name.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ranks "James" at 217 in popularity

If the girls (200 in number) are in the arraylist before the boys, then 200+17 = ...
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the girls (200 in number) are in the arraylist before the boys, then 200+17 = ...


That makes sense. I guess that's because I didn't do it in a seperate arrayList. Is it a big problem to fix it, if not, how would I go about doing that?

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If none of the boys' names are in the girls' list, subtract off the length of the girls' list.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:If none of the boys' names are in the girls' list, subtract off the length of the girls' list.

That would not meet the requirement: Write a Java application that reads two files into separate ArrayLists
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!