Hi I am having trouble with getting my program to run properly. I was able to clear any syntax errors, but now I am having issued with my output.
First of all, inside the first IF statement, it prompts for the person to enter their name and department at the same time, so that when it outputs, name is blank and only department has input. I'm thinking it's something with the whole IF statement, because if I change "String name" to input.next then name prompts correctly, but dept and totalHrsWkd get merged together.
Also, while testing my program, it crashes when I enter a negative number for totalHrsWkd. It will display the two print statements all on one line and then crash JCreator.
I'd appreciate any help on the matter, thank you!
public static void main(String[] args)
{
// TODO code application logic here
int attempt = 1, employeeID = 0;
double hoursWorked = 0.0;
double overtimeHrs = 0.0;
double totalHrsWkd = 0.0;
Scanner input = new Scanner(System.in);
while( attempt < 4 )
{
System.out.println( "Enter your employee ID: " );
employeeID = input.nextInt();
if( employeeID == 12345678 )
{
System.out.printf( "Enter your name: " );
String name = input.nextLine();
System.out.printf( "Enter your department: " );
String dept = input.nextLine();
System.out.printf( "Enter your hours worked including overtime: " );
totalHrsWkd = input.nextDouble();
while( totalHrsWkd < 0 )
{
System.out.printf( "Try again! Hours worked cannot be negative.");
System.out.printf( "Enter your hours worked including overtime: ");
}
overtimeHrs = totalHrsWkd - 40;
hoursWorked = totalHrsWkd - overtimeHrs;
if( overtimeHrs <= 0 )
{
}
else if( overtimeHrs == 0 )
{
}
else if( hoursWorked == totalHrsWkd )
{
}
else if( hoursWorked == 40 )
{
}
System.out.printf( "Name: %s\n" + "Dept: %s\n" +
"Hours Worked: %.2f\n" + "Overtime Hours: %.2f\n"
+ "Total Hours Worked: %.2f\n", name, dept,
hoursWorked, overtimeHrs, totalHrsWkd);
attempt = 3;
}
else
{
if(attempt < 3)
{
System.out.println( "Invalid ID! Try again." );
}
else
{
System.out.println( "Invalid ID! 0 attempts left. Exiting program!" );
}
}
++attempt;
}
System.exit(0);
}
}
totalHrsWkd = input.nextDouble();should be put inside the while loop. Otherwise you're just running in an infinite loop with nothing changingtotalHrsWkdScanner? That's almost never appropriate for interactive programs (and causes problems like you see here). Instead, useBufferedReader.readLine.