Calculating employee salary

• 02-07-2011, 03:11 AM
subiedude101
Calculating employee salary
I'm a mess. I can't seem to figure this out. Here is what I have to do:

Develop a Java application that will determine the gross pay for each of three employees. The company pays "straight-time" for the first 40 hours worked by each employee and pays "time-and-a- half" for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee, and should determine and display the employee's gross pay. Use class Scanner to input the data.

and here is the error I get when I attempt to run it:
Code:

```C:\Examples\ch04\Assignment4_20>java SalaryCalculatorTest Enter first employee name: Mike Enter the number of hours worked: 40 Enter the hourly rate: 10.00 Exception in thread "main" java.util.InputMismatchException         at java.util.Scanner.throwFor(Unknown Source)         at java.util.Scanner.next(Unknown Source)         at java.util.Scanner.nextInt(Unknown Source)         at java.util.Scanner.nextInt(Unknown Source)         at SalaryCalculatorTest.main(SalaryCalculatorTest.java:22)```
Here is what I have thus far:
Code:

```import java.util.Scanner; public class  SalaryCalculatorTest {         public static void main(String[] args)         {                 Scanner input = new Scanner( System.in);                 SalaryCalculator employee1 = new SalaryCalculator();               SalaryCalculator employee2 = new SalaryCalculator();                 SalaryCalculator employee3 = new SalaryCalculator(); //Employee #1 System.out.print( "Enter first employee name: " ); String name1 = input.nextLine(); employee1.setEmployeeName( name1 ); System.out.print( "Enter the number of hours worked: " ); int hours1 = input.nextInt(); System.out.print( "Enter the hourly rate: " ); double rate1 = input.nextInt(); employee1.calculatePay( hours1, rate1 ); employee1.displayEmployee(); System.out.println(); //Employee #2 System.out.print( "\nEnter the number of hours workes: " ); int hours2 = input.nextInt(); System.out.println( "Enter the hourly rate: " ); double rate2 = input.nextDouble(); employee2.calculatePay( hours2, rate2 ); employee2.displayEmployee();                         } }```
Code:

```public class SalaryCalculator { private String employeeName; private int hours; private double rate, pay; public void setEmployeeName ( String name ) {         employeeName = name; } public String getEmployeeName() {         return employeeName; } public double calculatePay( int hours, double rate ) {         if ( hours > 40 )         { int extraHours = hours - 40;         pay = ( 40 * rate ) + ( extraHours * rate );         }         else pay = hours * rate;         return pay;         } public void displayEmployee() {         System.out.printf( "Employee's name: %s", getEmployeeName() );         System.out.printf( "\nGross Salary: \$&.2f", pay ); }                 }```
• 02-07-2011, 03:35 AM
doWhile
Quote:

System.out.print( "Enter the hourly rate: " );
double rate1 = input.nextInt();
See the API for Scanner.nextInt, and the reasoning why this exception would be thrown...then look at the input of 10.00. Perhaps try an alternative like nextDouble