0

I am supposed to prompt for an employee name, pay, and hours worked. its works fine for employee 1, but when it gets to employee 2, it skips over asking for the name even though its the same code.

import java.util.Scanner;
public class Employees {
     public static void main(String args[]) {

         Scanner inputScanner = new Scanner (System.in);
         String e1;
         String e2;
         String e3;
         double pay1;
         double pay2;
         double pay3;
         int hours1;
         int hours2;
         int hours3;
         double paycheck1;
         double paycheck2;
         double paycheck3;

         double tax = .053;
         double takeHome = .947;

         System.out.println("What is the first employee's name?");
         e1 = inputScanner.nextLine();
         System.out.println("How much do you make?");
         pay1 = inputScanner.nextDouble();
         System.out.println("How many hours did you work?");
         hours1 = inputScanner.nextInt();
         paycheck1 = pay1 * hours1;



         System.out.println("What is the second employee's name?");
         e2 = inputScanner.nextLine();
         System.out.println("How much do you make?");
         pay2 = inputScanner.nextDouble();
         System.out.println("How many hours did you work?");
         hours2 = inputScanner.nextInt();
         paycheck2 = pay2 * hours2;



         System.out.println("What is the third employee's name?");
         e3 = inputScanner.nextLine();
         System.out.println("How much do you make?");
         pay3 = inputScanner.nextDouble();
         System.out.println("How many hours did you work?");
         hours3 = inputScanner.nextInt();
         paycheck3 = pay3 * hours3;

         System.out.println(e1 + " will make " + paycheck1 + " before taxes.");
         System.out.println(e1 +" will be taxed" + paycheck1 * tax + " dollars.");
         System.out.println(e1 +" will take home" + paycheck1 * takeHome + " dollars.");

         System.out.println(e2 + " will make " + paycheck2 + " before taxes.");
         System.out.println(e2 +" will be taxed" + paycheck2 * tax + " dollars.");
         System.out.println(e2 +" will take home" + paycheck2 * takeHome + " dollars.");

         System.out.println(e3 + " will make " + paycheck3 + " before taxes.");
         System.out.println(e3 +" will be taxed" + paycheck3 * tax + " dollars.");
         System.out.println(e3 +" will take home" + paycheck3 * takeHome + " dollars.");

         System.out.println("The company will withhold a total of " + (paycheck3 + paycheck2 + paycheck1) * tax + " dollars.");

    }
 }

Once i run it, i get to the point where it is asking for the second employee and it outputs this

"What is the second employee's name? How much do you make? "

Completely skipping letting me input the name.

  • 2
    And hint: read about arrays and loops. Using a1, a2, a3, to then go and repeat the same code three times, just using different names is ... not a good idea. – GhostCat Sep 05 '18 at 14:36
  • Additionally, you can declare your variables inline where you read the input for each. Instead of having `String e1;` and `e1 = ...` just do `String e1 = ...`. – Adam Sep 05 '18 at 14:42

0 Answers0