0

I am writing a program in my programming class in java. The program is a simple do/while loop that asks some questions using scanner class and a priming read with a Boolean value. When the loop iterates the first time, it works properly, but when it iterates a second time, it displays the first question and second question at once and only allows me to answer the second. I hope this is clear. Anyone have any insight? Code:

      /*
      * Programmed By: Cristopher Ontiveros
      * Date: 9/19/13
 * Description: Compute weekly salary and witholding for employees
 */
package project.one.cristopher.ontiveros;

import java.util.Scanner;
public class ProjectOneCristopherOntiveros {

     static Scanner sc = new Scanner(System.in);
     public static void main(String[] args) {
     String fName, lName;
     double rate=0, wPay=0, withholding=0;
     int option = 0;
     boolean badval = true;
         System.out.println("Welcome to the Pay Calculator");


     do{
        System.out.println("Enter employee first name: ");
        fName = sc.nextLine();
        System.out.println("Enter employee last name: ");
        lName = sc.nextLine();
        System.out.println("Enter employee hourly rate: ");
        rate = sc.nextDouble();
        wPay = (rate * 40);
        withholding = (wPay * .20);
        System.out.println("Employee " + fName + " " + lName + "\nHourly Rate: " + rate + "\nWeekly Pay: " + wPay + "\nWithholding Amount: " + withholding);


         System.out.println("Would you like to enter another employee? (1 = yes, 2 = no)");
     option = sc.nextInt();
     sc.nextLine()
     if(option == 1){
         badval = true;

     } else {
         badval = false;

     }
     }while(badval);


  }

}

chris
  • 85
  • 2
  • 11
  • possible duplicate of [Skipping nextLine() after use nextInt()](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint) – Rohit Jain Sep 19 '13 at 19:31
  • I know this has nothing to do with your question but for the love of god start naming your classes better :p – redFIVE Sep 19 '13 at 19:32
  • @redFIVE the class is named I can see the professor liking that so (s)he can tell whose homework is whose when it's printed out. – dkatzel Sep 19 '13 at 19:34
  • Name your root folder of your project something like that, not class names. Comment blocks work wonders for adding stuff like name, date, etc – redFIVE Sep 19 '13 at 19:35

2 Answers2

5

When you do sc.nextDouble();, nextDouble reads only the double and skips the \n which indicates the new line (Enter key hit by user), so it'll be read by the next nextLine()!

You should "swallow" this new line by adding another sc.nextLine().

Maroun
  • 87,488
  • 26
  • 172
  • 226
  • 1
    Better yet, never use `nextDouble()` and `nextInt()`. Just use nextLine() and `parseInt()`. For simple parsing I generally prefer to do it on my own, than depend on something else. `nextInt()` isn't entirely obvious what it does without reading the documentation – Cruncher Sep 19 '13 at 19:39
  • @Cruncher Yes We should not use `nextDouble()` and `nextInt()` methods until we have the inputs in a single line(separated by space) and after usage of them we have to call `nextLine()` . – Prabhaker A Sep 19 '13 at 19:47
1

The problem is with the sc.nextDouble() method .It reads next token of the input as a double and skips remaining.So you use nextLine() method when you use next() or nextInt() or nextDouble() ,etc methods.

Prabhaker A
  • 7,573
  • 1
  • 16
  • 24