3

I recently started studying Java and came across a problem while testing out something. This might be a very easy question, but I can't seem to solve it. This is my code:

    int firstj = 1;

    if (firstj == 1) {
        String choice = "Type a number between 1 and 4";
        System.out.println(choice);
        while (true) {

            if (firstj == 1) {
                Scanner third = new Scanner(System.in);
                String thirdch = third.nextLine();

                while (true) {

                    if (thirdch.equals("1")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    } else if (thirdch.equals("2")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    } else if (thirdch.equals("3")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    }

                    else if (thirdch.equals("4")) {
                        // I need this to break the loop and move on to the
                        // "Done." string
                        break;
                    }

                    else {
                        System.out.println("Type a number between 1 and 4");
                        thirdch = third.nextLine();
                    }
                }

            }
        }
    }
    String done = "Done";
    System.out.println(done);

I want to make it so that when you type 1, 2 or 3, you get the string telling you to type a number again and accept user input, while when you type 4 the loop breaks and goes to the String done. I would be grateful if you could help me solve this problem with an easy code, since I don't know any of the more advanced things.

3
  • Do you mean you want to break out of the outer loop? Commented Sep 30, 2015 at 15:07
  • You are breaking out of inner while, but not outer while loop. So while(true) is always true. Commented Sep 30, 2015 at 15:08
  • "break" can only get you out of 1 loop. Your most fundamental problem is the infinite loops. To have one infinite loop is already bad enough, you actually have 2 of them. It's time to refactor your code. Commented Sep 30, 2015 at 15:30

4 Answers 4

7

You can label loops, and then use the label in your break statement to specify which loop you want to break out of, e.g.

outer: while (true) {
  while (true) {
    break outer;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

5

The most scalable way of breaking out of a nested loop is to put the whole thing in a function and use return.

Break to a label in Java is the other option, but it can make code brittle: you are at the mercy of some recalcitrant refactorer who might feel inclined to move the "break to label" blissfully unaware of the consequences; a compiler is unable to warn of such shenanigans.

3 Comments

Perhaps you can give some examples of the brittleness to watch out for? Such 'unspecified badness' is quite hard to spot until it's bitten you.
I think Bathsheba was simply trying to point out this kind of usage of break statement can be hard to follow when more loops are added and the code becomes larger and more complex, and I totally agree with him.
@ChristopherYang And I don't disagree: I can't actually remember the last time I used break with a label; I think that there are usually simpler ways to structure the code to avoid it. My point was that it would be useful to people who aren't familiar with the sorts of problems it creates to describe what is meant by saying that the code is brittle.
2

You could use a lock to stop, like that:

public static void main(String[] args) throws Exception {
    int firstj = 1;
    if (firstj == 1) {
        boolean lock = true;
        while (lock) {

            if (firstj == 1) {
                Scanner third = new Scanner(System.in);

                while (lock) {

                    System.out.println("Type a number between 1 and 4");
                    String thirdch = third.nextLine();
                    switch (thirdch) {
                    case "1":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "2":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "3":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "4":
                        lock = false;
                        break;
                    }

                }
                third.close();

            }
        }
    }
    String done = "Done";
    System.out.println(done);
}

I hope it helps.

Comments

1

It isn't quite clear from the description, but continue will let you skip to the end of the loop, to continue with the rest of the loop, and you can use flags to control if you want to exit out of more than one level.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.