• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input issue  RSS feed

 
Miley Johnson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

got an issue with a scanner and don't know what the compiler is doing wrong:



I keep getting "n/a, pick another".
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is an Object in Java.
"==" operator can not be used to compare the value of 2 Strings.
You need to use equalsTo() method.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:String is an Object in Java.
"==" operator can not be used to compare the value of 2 Strings.
You need to use equalsTo() method.

You probably meant equals(), not equalsTo()
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you have to use equals() to compare values in the object

==  is a reference comparison, i.e. both objects point to the same memory location or not.

So please use equals().

As per my suggestion use should int instead of string because user is gone enter option number..!!!
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for using "should".I noticed it after I Posted it.
 
Miley Johnson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks =/ ^^
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you writing a String on the keyboard, converting it to an int with nextInt() and then converting it back to a String? If you want a String use the next() method for your Scanner.
Why are you using multiple ifs rather than a switch statement? Since Java7, switches have taken Strings as labels, as well as ints.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You probably meant equals(), not equalsTo()

Sorry...my bad.
This is probably the worst typo I have done in my life 
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaishakh Menon wrote: Sorry for using "should".I noticed it after I Posted it.

It's not that you can never use the word "should", it's just that we want you think about how you're using it.

"you should always use private variables when possible" is okay but better is "Use the most restrictive access modifier you can."  See how instead of using "should" you explain why it's good to do something?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:. . . This is probably the worst typo I have done in my life 
Don't worry about it. The compiler would have caught it, and you would have known about the error quickly. Nothing serious can go wrong with code which doesn't compile.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!