Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Input issue

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 16718
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks =/ ^^
 
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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  
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic