JVX
JVX

Reputation: 95

Java: Labeled Break

I am working on a practice exercise in my online pursuit to learn Java and am stumped!

The gist of my program is I have the user select an option via the input of a single char, then the program proceeds to cases based off of the value. If the default case executes, that means the input was invalid and I want to then return to the user input prompt.

I initial thought was to use a 'goto', however from what I understand, I would probably be stoned to death by anyone besides me reading the code. And then there's the fact that goto doesn't exist in Java... So while Googling, I found 'labeled breaks'. It looked just like what I needed. However, the spot which I have inserted the label is unrecognized, even though it's in the same class as the cases. How should I go about doing this?

String newLine = System.getProperty("line.separator");

  restart:

  System.out.println("Please select the type of shape you wish to calcuate information for: "
  + newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");

  char typeShape = input.next().charAt(0);

  String shape = Character.toString(typeShape);

  switch (shape.toLowerCase()) {
      case "a": 
          //do something
          break;
      case "b":
          //do something
          break;
      case "c":
          //do something
          break;
      default:
          System.out.println("Invalid selection. Please re-enter shape.");
          break restart;
  }

Upvotes: 3

Views: 2333

Answers (4)

Vince
Vince

Reputation: 15146

Labeled blocks are frowned upon for similar reasons goto is frowned upon: it's not a natural flow.

With that said, you might be wondering how you would manage the behavior you want, which is pretty simple: use a loop

//pseudo-code
while(something) {
    repeatCode
}

In your case, you would do something like:

boolean choosing = true;
while(choosing) {
    switch(...) {
        case "a":
            choosing = false;
            break;
        case "b":
            choosing = false;
            break;
    }
}

You may find this a bit verbose. Instead, you could set choosing to false as soon as you enter the loop, then set it back to true if the user didn't enter a correct name.

Or better yet, use a do-while loop:

boolean choosing = false;
do {
    switch(...) {
        case "a":
            break;
        default:
            choosing = true;
            break;
    }
} while(choosing);

Upvotes: 2

YoungHobbit
YoungHobbit

Reputation: 13402

I guess a simple approach will be to use the do-while loop. If the condition is not satisfied (invalid input/character), continue the loop, otherwise set the flag to false and come out.

boolean inputFlag;
do {
System.out.println("Please select the type of shape you wish to calcuate information for: "
    + newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");

  char typeShape = input.next().charAt(0);
  String shape = Character.toString(typeShape);

  switch (shape.toLowerCase()) {
    case "a":
      inputFlag = false;
      //do something
      break;
    case "b":
      inputFlag = false;
      //do something
      break;
    case "c":
      inputFlag = false;
      //do something
      break;
    default:
      System.out.println("Invalid selection. Please re-enter shape.");
      inputFlag = true;
  }
} while (inputFlag);

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

I believe you want to label a block. Something like

restart: {
    System.out.println("Please select the type of shape you wish to calculate "
      + "information for: " + newLine + "A: Square" + newLine + "B: Rectangle"
      + newLine + "C: Circle");

    char typeShape = input.next().charAt(0);

    String shape = Character.toString(typeShape);

    switch (shape.toLowerCase()) {
    case "a": 
        //do something
        break;
    case "b":
        //do something
        break;
    case "c":
        //do something
        break;
    default:
        System.out.println("Invalid selection. Please re-enter shape.");
        break restart;
    }
}

Upvotes: 5

Gili
Gili

Reputation: 90023

Java allows you to label a loop construct (e.g. for, while) and then jump out of the inside one of the loops to an outer level.

The language does not allow you to label arbitrary lines and "goto" them.

UPDATE: Apparently I was wrong. Java supports labeling arbitrary blocks (but not individual statements). See https://stackoverflow.com/a/1940322/14731

Upvotes: 3

Related Questions