Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: [Homework] Requesting User Input

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [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.

    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!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: [Homework] Requesting User Input

    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.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.

  4. The Following User Says Thank You to The_pizzaguy For This Useful Post:

    nKulo (January 24th, 2013)

  5. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: [Homework] Requesting User Input

    Quote Originally Posted by The_pizzaguy View Post
    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?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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().
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    nKulo (January 24th, 2013)

  8. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: [Homework] Requesting User Input

    Quote Originally Posted by Norm View Post
    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!

Similar Threads

  1. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  2. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  3. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  4. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM