import java.util.Scanner;
public class EmpTest {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// there is a second class with my constructors and other stuff of that nature
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
String no = "no";
String yes = "yes";
while (true) {
System.out.println("Please enter in the employee name");
e1.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e1.salary = input.nextDouble();
while (true) {
System.out.println("Would you like to add a second employee?");
String userInput = input.next();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e2.name = input.next();
System.out.println("Please enter in the salary of that employee");
e2.salary = input.nextDouble();
}
//this is where my break wont terminate the loop
//the console output just asks for the second employee salary and name without waiting for a user input
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
System.out.println("Would you like to add a third employee?");
userInput = input.next();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e3.name = input.next();
System.out.println("Please enter in the salary of that employee");
e3.salary = input.nextDouble();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
}
}
}
}
-
6Possible duplicate of [Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?](http://stackoverflow.com/questions/7877529/java-string-scanner-input-does-not-wait-for-info-moves-directly-to-next-stateme) – JonK Oct 27 '15 at 15:06
-
Very common problem with scanners. Calls like `nextInt` and `nextDouble` don't remove the newline character that the Enter key inserts into the input, so the next time you ask for any type of string, there's already a newline character in the input buffer, so it eats it up and moves on. – JonK Oct 27 '15 at 15:07
-
Thank you for the help everybody!! – bman Oct 29 '15 at 15:12
4 Answers
After my first wrong answer here's the correct thing. As by the JLS:
A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method
So in your case you are break
ing the inner while(true)
-loop.
If you want to break
some "outer" block you have to use break
with a label like this:
loop:
while(true) {
.....
while(true) {
...
break loop;
}
}

- 11,718
- 21
- 43
- 52
your break statement is breaking inner while loop but no break statement for outer while loop.

- 49
- 7
-
that observation is correct. That does not fully solve the problem yet, though... – Vogel612 Oct 27 '15 at 15:58
-
You have to provide 2 break statements, one for the inner while
loop and another for the outer while
loop.

- 616
- 6
- 17

- 649
- 2
- 8
- 20
Here is my answer with some explanations on the code:
import java.util.Scanner;
public class EmpTest {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
String no = "no";
String yes = "yes";
//initialized userInput here so that we can use it on the outer loop
String userInput="";
while (true) {
System.out.println("Please enter in the employee name");
e1.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e1.salary = input.nextDouble();
input.nextLine();
//nextDouble() reads only the double and doesn't finish the line, so we need to add nextLine() method after nextDouble()
while (true) {
System.out.println("Would you like to add a second employee?");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e2.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e2.salary = input.nextDouble();
//again
input.nextLine();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
System.out.println("Would you like to add a third employee?");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e3.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e3.salary = input.nextDouble();
input.nextLine();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
}
//do the remaining things
//add the condition for breaking the outer loop if there's any
break;
}
//never forget to close the scanner
input.close();
}
}
some notes:
nextDouble() reads only 1 double and doesn't finish the line, so I added input.nextLine() to finish the line.
I just added a break on the outer loop, right after the inner loop. I don't know if you have a condition for breaking the outer loop, or if there's anything that you want to do after breaking the inner loop
You can also convert that loop into method and just use "return" statement to break out of the method. I don't know if it's allowed though, so I just stick to how you created the program and modified it a bit.
Never forget to close the scanner after using it

- 719
- 12
- 31
-
please mark this as answer if it helped you. Btw, I like @piet t 's answer but I never recommend using labels, It's like using goto, and such labels can cause spaghetti code – triForce420 Nov 02 '15 at 10:09