Results 1 to 2 of 2
  1. #1
    subiedude101 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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();
    		
    	}
    }
    Java 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 );
    }
    	
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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

Similar Threads

  1. Salary Calculator
    By PeruanoJava in forum New To Java
    Replies: 13
    Last Post: 09-05-2010, 09:59 PM
  2. employee
    By srish in forum Suggestions & Feedback
    Replies: 7
    Last Post: 07-21-2010, 04:52 AM
  3. search employee id
    By raman_v in forum Java Servlet
    Replies: 2
    Last Post: 12-24-2009, 08:44 AM
  4. computing hour & Salary
    By arshesander in forum New To Java
    Replies: 4
    Last Post: 08-07-2009, 10:17 PM
  5. weekly salary
    By dollakay in forum New To Java
    Replies: 2
    Last Post: 05-09-2008, 02:46 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
  •