• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unique Names Problem  RSS feed

 
M. Haus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a java program that asks the user for a list of names (one per line) until the user enters a blank line. At that point the program should print out the list of names entered, where each name is listed only once (i.e., uniquely) no matter how many times the user entered the name in the program. I am supposed to use an ArrayList for this problem.

My idea for a solution is to create an ArrayList<String>, read each name, i.e., line, entered and then to see if that name is already in the ArrayList. I created a for loop to check each element in the ArrayList but when I try to assign an element to a string variable I get the error "Type mismatch: cannot convert from Object to String". Not sure why that is happening because the ArrayList is defined as a String list.

Here is the code so far. It is incomplete to solve the problem but does include the aforementioned for loop. Any help would be appreciated.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does readLine() works, you didnt showed us the code? To have unique name you can use hashset type of collection instead of list.
For reading the data you can use scanner.
 
M. Haus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
readLine is a java method that takes a string prompt as a parameter. The user will see the prompt and types in a text string and hits enter. My code assigns that string to the string variable "line". If the user doesn't enter any text and just hits enter the code breaks.

My thought is to check the variable line against any entries in the ArrayList sList (which is passed as a parameter); however, when I try to assign an ArrayList element (which is a type string) to a string variable so I can compare it to the string variable line, I get the error. Not sure why. The get method should return the ith element of ArrayList sList which should be a string. In my mind I am assigning a string to a string variable; however, the code says that cannot convert from object to string so it seems that the get method is returning the ArrayList object and not the element string? Or?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason is you are passing generic type of argument in readList which returns elements of type object. So either you have to
cast the element in to desired type or provide type in arguement.

So for 1st case you have to do like this:



And in 2nd case, you have to do like this:

 
M. Haus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Excellent. Does the trick

From the lectures I was watching (Stanford CS106A), it was presented that didn't have to pass the parameter type (in case of ArrayLists) because the compiler would know from declaration. I did notice, however, the use of typecast as you suggested in an example in the accompanying textbook which I did not understand. So now I do.

Thanks for the help.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference, another reply to this topic was provided in this duplicate thread.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
M. Haus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a java program that asks the user for a list of names (one per line) until the user enters a blank line. At that point the program should print out the list of names entered, where each name is listed only once (i.e., uniquely) no matter how many times the user entered the name in the program. I am supposed to use an ArrayList for this problem.

My idea for a solution is to create an ArrayList<String>, read each name, i.e., line, entered and then to see if that name is already in the ArrayList. I created a for loop to check each element in the ArrayList but when I try to assign an element to a string variable I get the error "Type mismatch: cannot convert from Object to String". Not sure why that is happening because the ArrayList is defined as a String list.

Here is the code so far. It is incomplete to solve the problem but does include the aforementioned for loop. Any help would be appreciated.

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how you defined your ArrayList in run():


This is the parameter used in the other two methods:


In the former you have told the compiler that the sList ArrayList holds Strings only, but in the latter you have only said it's a raw List, so essentially holds Objects.
Consequently when you get() an item out of the latter it is treated as an Object, hence the error.

Since those methods only want to be dealing with ArrayLists of Strings then you should define the parameter as such, in the same way you defined sList.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duplicate of this thread.

Please continue the discussion over there. Thanks.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:For reference, another reply to this topic was provided in this duplicate thread.
Have merged the two threads. I think this is the answer you mentioned, Tim.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!