[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!
Re: [Homework] Requesting User Input
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.
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.
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?
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().
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!