This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Input error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input error" Watch "Input error" New topic
Author

Input error

yakabuff bob
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Hi,

Recently, I tried making my own calculator but I ran into some errors...


I listed out my problem with comments in the code listed..

Any help would be appreciated

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Welcome to the Ranch Good thing you found the code button, but please indent the code inside the code tags, too.

Just before the offending line, add a line for testing which prints out the int which you have just read (I think snum). Does that give the right value?
What have you been told nextLine() does? Did anybody tell you it reads the next line? Does your book tell youit reads the next line? Becuase it doesn’t. It tells you something different here, but it may not be clear. If you read something and then call nextLine(), it reads the remainder of the line. As you will see from this message, it can even read a zero‑length line. Which doesn’t do you much good. In that sort of instance, you need the enter key be pressed before the prompt. The simplest thing to try to call nextLine() and discard its return value before trying to display the prompt.
yakabuff bob
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Can you please point me in a general direction of removing the return value of nextLine()?


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
No. You can’t remove the return value. You simply don’t use it. You write myScanner.nextLine(); as a whole line, and the return value disappears into a sort of cyber‑limbo never to be seen again.
Didn’t it say that in the old thread I quoted? Try here.
yakabuff bob
Greenhorn

Joined: Feb 09, 2013
Posts: 5
So I replace "myscanner".nextLine() with my own variable?
yakabuff bob
Greenhorn

Joined: Feb 09, 2013
Posts: 5
So do I just replace "myscanner".nextLine() with my own variable?


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
yakabuff bob wrote:So I replace "myscanner".nextLine() with my own variable?
Not quite. You might have to call input.nextLine() twice and only assign it to your own variable second time.
yakabuff bob
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Thanks for helping me

My book had always said that nextLine() was a scanner for strings though..

Guess I misinterpreted it..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You’re welcome
I told you a lot of books get that bit wrong.
 
jQuery in Action, 2nd edition
 
subject: Input error
 
Similar Threads
Can someone tell me what I am doing wrong?
Help me out with the simple calculator code Please!!!!
How to return values from a different class file to main?
Making Decisions Lottery application
try...catch blocks giving <identifier> expected error