Results 1 to 2 of 2
Thread: Calculating employee salary
- 02-07-2011, 02:11 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
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:
Here is what I have thus far: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)
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 ); } }
- 02-07-2011, 02:35 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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 nextDoubleSystem.out.print( "Enter the hourly rate: " );
double rate1 = input.nextInt();
Similar Threads
-
Salary Calculator
By PeruanoJava in forum New To JavaReplies: 13Last Post: 09-05-2010, 09:59 PM -
employee
By srish in forum Suggestions & FeedbackReplies: 7Last Post: 07-21-2010, 04:52 AM -
search employee id
By raman_v in forum Java ServletReplies: 2Last Post: 12-24-2009, 08:44 AM -
computing hour & Salary
By arshesander in forum New To JavaReplies: 4Last Post: 08-07-2009, 10:17 PM -
weekly salary
By dollakay in forum New To JavaReplies: 2Last Post: 05-09-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks