1. Member
Join Date
Jul 2010
Posts
5
Rep Power
0

## Salary Calculator

Hi everybody
I have a proble with the following code. Everytime it ask me for the second employee, it goes straight to the next question. Here is the code:

<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 );
}
}

------------------------------------------------------------------------

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 the employee #1 name: " );
String name1 = input.nextLine();
employee1.setEmployeeName( name1 );

System.out.print( "Enter the # of hours worked for this employee: ");
int hours1 = input.nextInt();

System.out.print( "Enter the hourly rate for this employee: ");
double rate1 = input.nextDouble();

employee1.calculatePay( hours1, rate1 );
employee1.displayEmployee();

// Employee #2
System.out.print( "\n\nEnter the employee #2 name: " );
String name2 = input.nextLine();
employee2.setEmployeeName( name2 );

System.out.print( "Enter the # of hours worked for this employee: ");
int hours2 = input.nextInt();

System.out.println( "Enter the hourly rate for this employee: ");
double rate2 = input.nextDouble();

employee2.calculatePay( hours2, rate2 );
employee2.displayEmployee();

}
}

<\code>

This is only 4 chapters in Java. Dont know that much.

Sorry I this is the description

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 input dialogs to input the data.

Thanks
Last edited by PeruanoJava; 07-13-2010 at 11:20 PM.

2. Please copy the contents of the command prompt window when you run the program and paste it here.
The Scanner class reads and buffers input so its possible for it to have the answer for the next input request before your code prints the request for it. Play around with changing how you use Scanner to read in data to get it to work how you want.

3. Member
Join Date
Jul 2010
Posts
5
Rep Power
0
Hi Norm
Thanks for you reply. This is what happens when I run the code

Enter the employee #1 name: John Smith
Enter the # of hours worked for this employee: 40
Enter the hourly rate for this employee: 10
Employee's name: John Smith
Gross Salary: \$400.00

Enter the employee #2 name: Enter the # of hours worked for this employee:

It skipped the next question. I understand what happens. Because I am using "nextLine" in the String, everytime I push ENTER it is reading the key and that is why is skipping. My problem is how do I fixed. I been looking in the Scanner class, and I found a "Skip". Should I use it> I dont know how?

Thanks

4. Experiment some more with the Scanner class methods. You need to find the right combination of methods to be called to get what you want.
Try adding a nextLine() call after the nextDouble() call to clear the new line character that's in the buffer from when you pressed Enter on the line with the hourly rate.

Add some println() statements after every call to a Scanner method to show what was returned. Something like the following:
System.out.println("name2=" + name2);

Java Code:
` and`
not <code> and </code> . Any uncertainties in this regard are usually answered by looking at the forum FAQ's.

Now on to your problem -- likely it is because you are not calling Scanner#nextLine() enough. After you have scanned in next() or nextInt() or nextDouble(), if you are at the end of the line, call nextLine() on your scanner so that it will swallow the end of line character.

edit: dang connection froze on me, so this was late. Please see what Norm posted above.

6. Member
Join Date
Jul 2010
Posts
5
Rep Power
0
Sorry guys

I cannot figured out this one. How do I add another nextDouble()?

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 the employee #1 name: " );
String name1 = input.nextLine();
employee1.setEmployeeName( name1 );

System.out.print( "Enter the # of hours worked for this employee: ");
int hours1 = input.nextInt();

System.out.print( "Enter the hourly rate for this employee: ");
double rate1 = input.nextDouble();

employee1.calculatePay( hours1, rate1 );
employee1.displayEmployee();

System.out.println();

// Employee #2
System.out.print( "\nEnter the employee #2 name: " );
String name2 = input.nextLine();
employee2.setEmployeeName( name2 );

System.out.print( "Enter the # of hours worked for this employee: ");
int hours2 = input.nextInt();

System.out.println( "Enter the hourly rate for this employee: ");
double rate2 = input.nextDouble();

employee2.calculatePay( hours2, rate2 );
employee2.displayEmployee();

}
}```
I add another println() and didnt do it. I know I almost got it. I just need to clear the buffer from the Enter.

I almost forgot the other class

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 );
System.out.println();
}
}```

7. How do I add another nextDouble()?
You've got 2 now, so you know how to code them.
Can you explain why adding another is a problem?

I just need to clear the buffer from the Enter.
Did you read post#4 or post#5?

8. Member
Join Date
Jul 2010
Posts
5
Rep Power
0
Originally Posted by Norm
Experiment some more with the Scanner class methods. You need to find the right combination of methods to be called to get what you want.
Try adding a nextLine() call after the nextDouble() call to clear the new line character that's in the buffer from when you pressed Enter on the line with the hourly rate.

Add some println() statements after every call to a Scanner method to show what was returned. Something like the following:
System.out.println("name2=" + name2);
Hi Norm
Yes I did read them. I cannot figured out how to add a "nextLine()" call after the "nextDouble()" call.

Thanks for the quick response

Sorry I kind of new in Java.
My understanding is the nextLine() will receive a String. I have a nextLine() right after nextDouble() for employee #2. So that is why I am confused. I dont know if I got the right concept for nextLine().
Last edited by PeruanoJava; 08-06-2010 at 03:17 AM.

9. Write a smalll program that uses nextLine and nextDouble and hasNext and hasNextDouble.
Try using the various methods in different orders. Printout what is read in for each.
Try executing the program and giving it different inputs and see what happens.

For example:
System.out print("Enter a double:");
double d1 = nextDouble();
System.out.println("d1=" + d1);
String x1 = next();
System.out.println("next="+ next);
String x2 = nextLine();
System.out.println("x2=" + x2 + "<");

etc

Change the order and use different next methods and print them out to see how to use them. Also try entering more than one thing on a line.

10. Member
Join Date
Jul 2010
Posts
5
Rep Power
0
GOT IT!!!!!
Thanks Norm, I finally got it. I really appreciate all help given here. Thanks guys!!!!!:)

11. Member
Join Date
Sep 2010
Posts
2
Rep Power
0

## Salary calculator

Please share the final source code

12. Member
Join Date
Sep 2010
Posts
2
Rep Power
0
PeruanoJava --Please share the ammended Class SalaryCalculatorTest

13. JavaMtoto - IF you need help, start a new thread with your questions/problems.

14. Originally Posted by Norm
JavaMtoto - IF you need help, start a new thread with your questions/problems.
Agree. No use waking a zombie. Let's lock this old thread.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•