John Fraser

Greenhorn
+ Follow
since Oct 22, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Fraser

Hi

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

Thanks,
John
19 years ago

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
19 years ago

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
19 years ago
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

19 years ago