• Post Reply Bookmark Topic Watch Topic
  • New Topic

input.next and input.equals()  RSS feed

 
Vadim Melders
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would like to ask for correction as I can not make this work :



When I input "a" nothing comes out. But my idea is to give an answer.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the problem is the strange behavior of Scanner and System.in. You ask for a double with nextDouble() and you enter a double and an <Enter>. The second nextDouble() skips past the first <Enter> to get the next double, but there is still the second <Enter> in the buffer. Unfortunately, next() reads this character and returns, giving you an empty string.

To avoid this, call a nextLine() method before the next(). This will consume the <Enter>.
 
Vadim Melders
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This worked for me. Still I don't understand how Scanner.equals() works.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vadim Melders wrote:Still I don't understand how Scanner.equals() works.


I don't either, but then why would you want to call the equals() method of a Scanner object? My guess is that it could only be equal to itself, but if that isn't the case then the API documentation would surely explain why not. However I doubt you're ever going to need that.
 
Vadim Melders
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have spent not more than 5 hrs learning java so far. A lot of things to learn, so I believe by asking simple questions you can learn a bit and get some simple advice
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Then advice for beginners: don't bother with Scanner.equals(), it doesn't do anything you need.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I believe the problem is the strange behavior of Scanner and System.in. You ask for a double with nextDouble() and you enter a double and an <Enter>. The second nextDouble() skips past the first <Enter> to get the next double, but there is still the second <Enter> in the buffer. Unfortunately, next() reads this character and returns, giving you an empty string.

To avoid this, call a nextLine() method before the next(). This will consume the <Enter>.
Not convinced. It is nextLine() which would cause that problem. If you call next, that skips over all instances of the delimiter until it finds something non‑delimiter. The default delimiter is multiple whitespace, which includes space, newline, etc. So next() will find the a for addition. I think Paul C has identified the problem correctly; OP is calling equals() on the wrong object.
But I think I prefer Vadim Melders' suggestion. But you don't need 1 2 3 4; you can use Strings after case (in Java7+).
 
Vadim Melders
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: But you don't need 1 2 3 4; you can use Strings after case (in Java7+).

Thanks, useful link.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!