Reputation: 1
I am having two issues with a bit of simple code I am trying to write for my java class. Sorry if this seems easy to most of you, I am just starting with Java. the first issue i am having is the loop doesnt continue properly. It runs once just fine, and will exit if "quit" is entered at the start, but after it runs once, it wont let the use enter another value for emp name. it goes straight from the last line enter emp name to the first line of the loop, enter hourly rate, with no chance to enter a new name or quit.
My second issue, is my if statements are trying to make sure teh values entered for hourly rate and hours worked are greater then 0. If 0 is entered it gives the error message just fine, but if a negative number is entered the program continues like nothing is wrong. How do i make sure the value entered is ONLY a positive double, and that it wont accept a negative double or zero?
// Program to Calculate Payroll
import java.util.Scanner;
import java.text.DecimalFormat;
class Payroll
{
public static void main(String args[])
{
Scanner myScanner = new Scanner(System.in);
String empName;
double hourlyRate;
double hoursWorked;
double grossPay;
double netPay;
double taxes;
DecimalFormat money = new DecimalFormat("$.00");
//Get User Information
System.out.print( "Enter employee name or enter 'quit' when finished. " );
empName = myScanner.nextLine();
while (!empName.equals("quit"))
{
System.out.print( "What is their hourly rate? $");
hourlyRate = myScanner.nextDouble();
if (hourlyRate <= 0)
{
System.out.println( "Value is not valid, please enter an amount above zero.");
System.out.print( "What is their hourly rate? $");
hourlyRate = myScanner.nextDouble();
}
System.out.print( "How many hours did they work? ");
hoursWorked = myScanner.nextDouble();
if (hoursWorked <= 0)
{
System.out.println( "Value is not valid, please enter an amount above zero.");
System.out.print( "How many hours did they work? ");
hoursWorked = myScanner.nextDouble();
}
//Calculate Pay and Taxes
grossPay = hourlyRate * hoursWorked;
taxes = .13 * grossPay;
netPay = grossPay - taxes;
//Display All Information
System.out.print( "Employee name: ");
System.out.println(empName);
System.out.print( "Hourly Rate: ");
System.out.println(money.format(hourlyRate));
System.out.print( "Hours Worked: ");
System.out.println(hoursWorked);
System.out.print( "Gross Pay = ");
System.out.println(money.format(grossPay));
System.out.print( "Federal Taxes = ");
System.out.println(money.format(taxes));
System.out.print( "Net Pay = ");
System.out.println(money.format(netPay));
System.out.print( "Enter employee name or enter 'quit' when finished. " );
empName = myScanner.nextLine();
}
System.out.println( "Thank you for using this payroll program.");
}
}
Upvotes: 0
Views: 1387
Reputation: 9935
it wont let the use enter another value for emp name.
because you got problem with the line
hoursWorked = myScanner.nextDouble();
Since this only takes the double value but not the \n
value. So, at the end, when program moves to the line
empName = myScanner.nextLine();
It will continue to take the \n
for the empName, and skip the input from keyboard
You need to put another myScanner.nextLine() before asking input to the empName value.
myScanner.nextLine();
System.out.println("....");
empName = myScanner.nextLine();
For the validation, just do like the other suggested
Upvotes: 0
Reputation: 533880
it wont let the use enter another value for emp name.
You only ask for the employee name once as it's not inside the loop. Move the code to ask the name inside the loop. You might fined adding the code to the end of the loop simplest, but I prefer to do a check at the start with a break.
How do i make sure the value entered is ONLY a positive double, and that it wont accept a negative double or zero?
You only ask the user to correct the number once. If you use a while loop it will keep asking until the rate is ok.
Upvotes: 0
Reputation: 137
In response to your first issue, you are not re-asking for the employee name within the while
loop. You should initialize the empName
outside the loop, but also ask again every time you loop.
String empName = "";
while (!empName.equals("quit")) {
//Get User Information
System.out.print( "Enter employee name or enter 'quit' when finished. " );
empName = myScanner.nextLine();
if (empName.equals("quit")) {
break;
}
For your second issue, you can use a while
loop again and check to make sure the hourlyRate
is greater than 0
. Just make sure to re-prompt every time it loops back to check.
hourlyRate = -1;
while (hourlyRate <= 0) {
System.out.print( "What is their hourly rate? $");
hourlyRate = myScanner.nextDouble();
if (hourlyRate <= 0) {
System.out.println( "Value is not valid, please enter an amount above zero.");
}
}
Upvotes: 2