wood burning stoves*
The moose likes Beginning Java and the fly likes Problem with keyboard input using a Scanner object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with keyboard input using a Scanner object" Watch "Problem with keyboard input using a Scanner object" New topic
Author

Problem with keyboard input using a Scanner object

John Fraser
Greenhorn

Joined: Oct 22, 2003
Posts: 4
Hi

Hello from cold and snowy Toronto. This is my post. I hope that you can help
me.

I am in the process of writing a simple console application that asks the user to input 3 pieces ( first name, last name and salary ) of data for an employee
and then display the information of the screen. I have to do this for 2 employees.

I create an Scanner object, ask the user for the first employee information,
ask the user for the second employee information, and output them to the
screen. The problem that I am experiencing is when I am prompting the
user for second employee's first name, it automatically skips to
the prompt for the last name.

One thing that is odd is that when I comment out the code that prompts the
user for the first employee's salary, I have no problems with the prompt for
the second employee's first name

How do I fix this problem?

The code is posted below.

Thanks,
John

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Scanner class is new to Java 5.0. I don't think too many people here are completely savy with all the new features. In fact, I've never used Scanner before, but perhaps I can give you some ideas about where to look. First you should bookmark the Java 5.0 API documentation. To find the Scanner class docs, scroll the upper left frame until you find the java.util package. When you click on it, the lower left frame loads with a list of all the classes and interfaces from that package. Scroll down until you find Scanner and click on it. This loads in the documentation in the main frame on the right.

Learning to navigate and use the Java API docs is an essential tool for the savvy Java programmer. You can read the detailed description about the class at the beginning. Futher down, you will find a list of all the available methods.

It sounds like there are problems when trying to read the numeric value for salary. Everything else is Strings as far as I can tell, sot here shouldn't be any major issues there. To find out the problem, you can call the ioException() method on the Scanner and print out a diagnostic:

Try this and see if it gives any useful information. If you have any difficulty understanding the output, copy-and-paste it here and we'll help you decipher it.

HTH

Layne


Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

My understanding is that Scanner is intended to read tokens, and by default these tokens are delimited by whitespace. My impression (from the API) is that the nextLine method is actually a way to skip past any remaining tokens on the current line, and reset to the next line. It then returns a String of everything that was skipped.

In contrast, the nextDouble method does not reset to the next line. So after calling nextDouble, when you then call nextLine, it will skip over what comes after the double (looking for another token) and assign that String to firstName.

Note that the code below prints the input. When it asks you for the salary, see what happens when you input "1234.56 fred".

So to get the code to work as you expect... After the nextDouble method is called, I think you need to call nextLine in order to reset to the next line. (Note that the return doesn't have to be assigned to anything. Simply adding a line of "input.nextLine();" should do it.)
[ February 12, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John Fraser
Greenhorn

Joined: Oct 22, 2003
Posts: 4
Originally posted by Layne Lund:

It sounds like there are problems when trying to read the numeric value for salary. Everything else is Strings as far as I can tell, sot here shouldn't be any major issues there. To find out the problem, you can call the ioException() method on the Scanner and print out a diagnostic:

Try this and see if it gives any useful information. If you have any difficulty understanding the output, copy-and-paste it here and we'll help you decipher it.

HTH

Layne[/QB]


Hi Layne

I tried what you suggested. The input.ioException() method is returning a null value which is telling me that there is not an exception.

The problem is that I do not have a chance to respond to the prompt for the second employee's first name which comes right after the prompt for the first employee's salary. If I comment out the prompt for first employee's
salary, the prompt for the second employee's first name works correctly.

If you have a chance, please run the code that I posted and you will see what I mean.

Thanks,
John
John Fraser
Greenhorn

Joined: Oct 22, 2003
Posts: 4
Originally posted by marc weber:
My understanding is that Scanner is intended to read tokens, and by default these tokens are delimited by whitespace. My impression (from the API) is that the nextLine method is actually a way to skip past any remaining tokens on the current line, and reset to the next line. It then returns a String of everything that was skipped.

In contrast, the nextDouble method does not reset to the next line. So after calling nextDouble, when you then call nextLine, it will skip over what comes after the double (looking for another token) and assign that String to firstName.

Note that the code below prints the input. When it asks you for the salary, see what happens when you input "1234.56 fred".

So to get the code to work as you expect... After the nextDouble method is called, I think you need to call nextLine in order to reset to the next line. (Note that the return doesn't have to be assigned to anything. Simply adding a line of "input.nextLine();" should do it.)

[ February 12, 2005: Message edited by: marc weber ]



Hi, Marc

I added the line that you suggested - "input.nextLine();" and it works. I did read the Scanner API before I made my first post and my thoughts were the same as yours. I needed to reset it to next line because of the way the
nextDouble() method works. I just didn't know how to reset it to the next line.

I do have another question, though. Below is the line that I added.

input.nextLine();

According to the API, this method returns a string but I am not assigning
it to anything. Why does it work? I would have thought that this line would generate an error either at compile time or run time. I would have thought that the line would have to look like the one below.

String dummy = input.nextLine();

That way the string that is being returned from this method has a place to go.

Thanks,
John
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The reason that you need the nextLine() method is that nextDouble() only reads the double value, but it leaves the return on the stream since it's not part of a double. When you then asked for the next employee's first name, it took that return and returns an empty String.

In Java (and C/C++ and a lot of other languages) it is acceptable to ignore the return value from methods/functions. Sometimes you just want the side effect and not the result. This is a perfect example.
John Fraser
Greenhorn

Joined: Oct 22, 2003
Posts: 4
Hi

Thank you Layne, Marc, and David for all your assistance. It was very helpful
and I learned a lot.

Thanks,
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with keyboard input using a Scanner object
 
Similar Threads
Setter/Getter method issues
Code problems
Identifier expected
Error problems
Problem With Method and Interface