Although a loop with nested if / else statements

Hi, I have problems running my program. I was able to clear any syntax errors, but now I have given my output.

First of all, inside the first IF statement, he requests that the person simultaneously enter his name and department, so when it is displayed, the name is empty, and only the department has an input. I think this is something with the whole IF statement, because if I changed the "String name" to input.next, then the name will ask correctly, but dept and totalHrsWkd will be combined together.

Also, during testing of my program, it is reset when I enter a negative number for totalHrsWkd. It will display two print statements on the same line, and then JCreator crashes.

I would be grateful for any help in this matter, thanks!

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); } } 
+4
source share
3 answers

The problem is

  employeeID = input.nextInt(); 

only reads the next sequence of digits from the input stream. However, in order to actually enter the employee ID, you also had to press Enter. Pressing the Enter key is still awaiting input, so when you are on the next call

  String name = input.nextLine(); 

the Scanner class goes into "oh, press Enter here, I think the user needs an empty name". You are not even given the opportunity to enter a name, so it seems that your program asks two questions at the same time (name and department).

If you want to continue to use the Scanner class, I would recommend avoiding methods like nextInt() and always use nextLine() . You, of course, will need to convert the number entered by the user into a Java integer using something like parseInt() .

+3
source
 attempt = 3; // Seems suspicious attempt++; // I think it more appropriate in loops of this kind than ++attempt 
+2
source

The reason your program does not work when totalHrsWkd less than 0 is because the following code creates an infinite loop.

  while ( totalHrsWkd < 0 ) { System.out.printf( "Try again! Hours worked cannot be negative."); System.out.printf( "Enter your hours worked including overtime: "); } 

You should also read the number of hours worked in this cycle.

+1
source

Source: https://habr.com/ru/post/1397669/


All Articles