jamesbond007
Diamond Member
Hey guys! I need some help with my code. I want to make this program fool-proof by providing if statements to help with program faults, user input errors, etc.
Here's what I have so far:
The line with the comment on there next to hours is where I need the help...I need to assign something or create a variable/object telling the program that if the hours are less than 0 (such as a negative integer), the program will detect the input error and cause it to say that there was an invalid input dected.
Please help or give any links or any thoughts of possible fixes...I'm stuck right now as this thing works fine and compiles, but I need help with that input. I'll be re-creating the if statements for the wage input as well, in case the user inputs a negative integer in there as well.
Thanks in advance!
~Travis W.
Here's what I have so far:
import java.util.Scanner;
public class ITSwages
{
public static void main(String[]args)
{
Scanner inputRate = new Scanner(System.in);
System.out.print("Please enter your hourly wage, excluding a dollar sign($): ");
double rate = inputRate.nextDouble();
Scanner inputHours = new Scanner(System.in);
System.out.print("Please enter the number of hours worked: ");
double hours = inputHours.nextDouble();
if (hours > 40.000) {
hours = (1.5*rate)*(hours - 40.000)+(40 * rate);
} else if (hours <= 40.000) {
hours = rate*hours;
} else if (hours < 0) {
// --> hours =
System.out.printf("Total Salary : $%5.2f" , hours);
System.out.println("");
}
}
The line with the comment on there next to hours is where I need the help...I need to assign something or create a variable/object telling the program that if the hours are less than 0 (such as a negative integer), the program will detect the input error and cause it to say that there was an invalid input dected.
Please help or give any links or any thoughts of possible fixes...I'm stuck right now as this thing works fine and compiles, but I need help with that input. I'll be re-creating the if statements for the wage input as well, in case the user inputs a negative integer in there as well.
Thanks in advance!
~Travis W.