Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Scanners put into if statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanners put into if statements" Watch "Scanners put into if statements" New topic
Author

Scanners put into if statements

Andy Hammersmith
Greenhorn

Joined: Sep 30, 2012
Posts: 13
I was curious on why when i try to use this method in my driver it wont accept what i type in as either the If or the Else If.
Am I programing something wrong? Because I've been working on this for hours and cant get it.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Why do you compare Strings with == operator? You should use equals instead.

This is the preferred way to test for equality, which prevents NullPointerException to be thrown.

Edit: Those long lines in your code really spoil the layout of the page. You could edit your original post and make those lines shorter.


The quieter you are, the more you are able to hear.
Andy Hammersmith
Greenhorn

Joined: Sep 30, 2012
Posts: 13
That worked wonderfully. Thank you so much.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You are welcome.

Do you understand the difference between == operator and equals() method? Make sure you understand why your code didn't work.
Andy Hammersmith
Greenhorn

Joined: Sep 30, 2012
Posts: 13
No, not fully. Could you explain it for?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Andy Hammersmith wrote:No, not fully. Could you explain it for?

I could, but the question has been asked quite a times here so a little search will probably give you a satisfactory answer (link).
Andy Hammersmith
Greenhorn

Joined: Sep 30, 2012
Posts: 13
Thank you so much, again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Another link about ==
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scanners put into if statements
 
Similar Threads
Can't Break Out of a Loop
Write a program that reads and writes from binary or text files
Doubt on a program using Scanner
method help
help me correct my program