Results 1 to 3 of 3
  1. #1
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default while loop bypasses scanner input on 2nd pass

    I'm new to Java and in my first class. Some of you will recognize the homework from one of the more known online universities. I'm not asking for anyone to do my homework but I could use some help in finding out why my application is acting the way it is.

    Below is the payrolltest class that I've created. I use a 'while' loop so once the application has done its job it will loop back and run through the process until "stop" is entered as the employee's name.

    So here's the code (yes I know it's a mess, but I'm learning). The problem area is in red. I have posted the results of running the application at the bottom of the code.

    /*
    * Program Class Name: Payroll
    * Payroll.java
    * Payroll.class
    *
    * This is a class that is part of a non-GUI based Java application. This
    * class will prompt and accept input from the user.
    *
    * Author: Todd
    * Date: March 26th, 2009
    */

    import java.util.Scanner; // import the scanner class
    public class payroll // set as class payroll
    {
    public void ScanInput() // set as ScanInput for use by main class
    {

    Scanner scannerInput = new Scanner(System.in); // initialize scanner input

    String employeeName = ""; // set employeeName to nothing.
    while(!employeeName.equalsIgnoreCase("stop"))
    {
    // prompt user for employee name and accept input
    System.out.println("Please enter the employees first and last name or the word 'stop' to exit the program.");
    employeeName = scannerInput.nextLine();

    if(!employeeName.equalsIgnoreCase("stop")) // if employeeName = stop then skip all this
    {
    double employeeRate = 0; // declare variable
    while(employeeRate <=0) // setup test and loop if input is negative
    { // prompt user for employee hourly rate and accept input
    System.out.println("Please enter the hourly rate for " + employeeName + ". Example: 32.25");
    employeeRate = scannerInput.nextDouble();

    if(employeeRate <=0) // test for negative number or zero. Tell user not to do that if negative found
    {
    System.out.println("Please use positive values only.");
    } // end if
    } // end while

    double employeeHours = 0; // declare variable
    while(employeeHours <=0) // setup test and loop if input is negative
    { // prompt user for employee hours worked and accept input
    System.out.println("Please enter the hours worked by " + employeeName + ". Example: 72.5");
    employeeHours = scannerInput.nextDouble();

    if(employeeHours < .1) // test for negative number or less than .1. Tell user not to do that if found
    {
    System.out.println("Please use positive values only.");
    } // end if
    } // end while

    // format display, calculate and display on screen
    System.out.printf("\nEmployee " + employeeName + " is paid $" + employeeRate + " per hour and worked " + employeeHours + " hours this week.\n");
    System.out.printf("Employee " + employeeName + " is to be paid $%.2f.\n\n", (employeeRate * employeeHours));

    } // end if for 'stop' for input of data - in other words, skip it if employeeName = stop
    } // end of while 'stop' test
    scannerInput.close(); // close scanner class
    } // end ScanInput
    } // end payroll



    RESULTS:
    run:
    Please enter the employees first and last name or the word 'stop' to exit the program.
    John Doe
    Please enter the hourly rate for John Doe. Example: 32.25
    32.50
    Please enter the hours worked by John Doe. Example: 72.5
    40

    Employee John Doe is paid $32.5 per hour and worked 40.0 hours this week.
    Employee John Doe is to be paid $1300.00.

    Please enter the employees first and last name or the word 'stop' to exit the program.
    Please enter the hourly rate for . Example: 32.25


    Everything in green is correct. The red is the error. As you can see the program is prompting for a new employee name, but the scanner input is bypassed and next the user is prompted for the hourly rate. This doesn't make sense to me. I have been un-able to find anything in the forums like this.

    Any help is greatly appreceated.

    Todd

  2. #2
    xf021209 is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default Found the problem

    Never mind, I found the problem and resolved it.

    /new-java/8676-scanner-input-problem.html

    Dang, this forum has already paid for itself! :)

    Todd

  3. #3
    hhh80p is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default can you explain it please?

    I have the same problem with my code:

    public void letsPlayPoker()
    {
    // Create Scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    String pokerName;
    int numberCards = 1;
    int counter = 0;
    int cardNumber;


    while (counter <= 2) // how many poker player do we have
    {
    System.out.print("Please enter the name of the poker Player: ");
    pokerName = input.nextLine();
    System.out.printf("Welcome:\n%s\n", pokerName);
    counter = counter + 1;

    while (numberCards <= 2)
    {
    System.out.print("\nEnter card number\n");
    cardNumber = input.nextInt();
    System.out.printf("name", cardNumber);
    numberCards = numberCards + 1;
    }
    if (numberCards == 3)
    numberCards = 1;
    }
    System.out.print("\nGood luck gentlemen!!!!\n");

    The first time it loop, it ask me for the name of the poker player, but in the second and third time, it print the system.out.print but it doesn't let me to input the name, and go straight to the cards number.

    can u help me please!

Similar Threads

  1. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM
  2. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  3. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  4. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 AM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •