# [Homework] Requesting User Input

• January 23rd, 2013, 10:26 PM
nKulo
[Homework] Requesting User Input
Hello all!

Brief background - College student. First course in programming. Trying to learn Java. We are using Eclipse for our IDE.

What this program is supposed to do: Have a user calculate a payroll by inputting information through a keyboard.

 Name Hours Worked This Week Hourly Pay Rate Paul Dickson 35 18.36 Mary Ann Ginsburg 40 21.40 David Cheong 40 12.00

Problem: When I prompt the user for information it errors on the second block of information. The first set works great. They enter: Paul Dickson [return], 35 [return], and 18.36 [return]. No problems. But for some reason when you try and enter on the next set: Mary Ann Ginsburg it errors.

Code :

```import java.util.Scanner;   public class Payroll {   /** * @author * @purpose * @date */   public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a name: "); String n1 = keyboard.nextLine(); System.out.println("Now we will calculate weekly earnings for: " + n1); System.out.println("Please enter the amount of hours worked this week: "); int h1 = keyboard.nextInt(); System.out.println("Thank you. Now please enter the current hourly pay rate: "); double pr1 = keyboard.nextDouble(); System.out.println(n1 + " should be making \$" + h1 * pr1 + " before deductions.");     System.out.println(); System.out.println("Please enter a name: "); String n3 = keyboard.nextLine(); System.out.println("Now we will calculate weekly earnings for: " + n3); System.out.println("Please enter the amount of hours worked this week: "); int h2 = keyboard.nextInt(); System.out.println("Thank you. Now please enter the current hourly pay rate: "); double pr2 = keyboard.nextDouble(); System.out.println(n3 + " should be making \$" + h2* pr2 + " before deductions.");   } }```

I would like to thank anyone in advance for taking the time to look at this and offering help!
• January 23rd, 2013, 10:45 PM
pbrockway2
Re: [Homework] Requesting User Input
Quote:

it errors
Do you mean there's an exception? If so post the whole stack trace (the thing that is displayed when the exception occurs). The first thing is to establish when - on what statement - the exception occurred.
• January 24th, 2013, 04:31 AM
The_pizzaguy
Re: [Homework] Requesting User Input
I didnt get any errors wen I was running your code. But i saw a problem that in the second blok of code the name got skipped.
This is because you use keyboard.nextLine(); instead you should use keboard.next(); for normal behavoir to get a string.
the .nextLine behaviors diffrent than you want it to.
• January 24th, 2013, 11:39 AM
nKulo
Re: [Homework] Requesting User Input
Quote:

Originally Posted by The_pizzaguy
I didnt get any errors wen I was running your code. But i saw a problem that in the second blok of code the name got skipped.
This is because you use keyboard.nextLine(); instead you should use keboard.next(); for normal behavoir to get a string.
the .nextLine behaviors diffrent than you want it to.

Thanks pizzaguy, that was the solution. Why wouldn't the nextLine method work though?
• January 24th, 2013, 12:31 PM
Norm
Re: [Homework] Requesting User Input
nextLine() reads the end of line character that was left in the Scanner class's buffer after the nextInt() method was used. That's a very common problem when using the Scanner class's methods when mixing calls to nextInt() or other next...() methods with calls to nextLine().
• January 24th, 2013, 12:42 PM
nKulo
Re: [Homework] Requesting User Input
Quote:

Originally Posted by Norm
nextLine() reads the end of line character that was left in the Scanner class's buffer after the nextInt() method was used. That's a very common problem when using the Scanner class's methods when mixing calls to nextInt() or other next...() methods with calls to nextLine().

Okay thank you for explaining!