ttt
ttt

Reputation: 39

How to break out of loop when case is selected

I have a code that has 4 cases and I am trying to break the loop and if the 'f' case is chosen. and then choose from that case. When i try to do the if statement with the break over 30 errors but when I take it away the code is fine.

String one = "";
boolean yea = true;   
Scanner sw = new Scanner(System.in);
while (yea == true)
{
    System.out.print(MENU);
    one  = sw.next();
    char choice  = one.charAt(0);
    switch(choice)
    {
        case 'f':
            friendsList();
            break; 
        case 'w':
            wall();
            break;
        case 'p':
            network();
            break;

        case 'q' : 
            yea = false;
            break; 
        default:
            System.out.println("Error: You have entered " + choice + 
            ". Please try again");

    }
}
if (case == 'f')
    {
    break;
    }
}

Upvotes: 0

Views: 163

Answers (5)

Andre Jenkins
Andre Jenkins

Reputation: 1

The if statement should be moved inside of the while loop to be effective, and case inside the if statement should be changed to choice.

so

  While(yea==true)
  {
        System.out.print(MENU);
        one  = sw.next();
        char choice  = one.charAt(0);

        if(choice == 'F')
        {
              break;
        }
        switch(choice)
        {
          //cases          
        }
}

Upvotes: 0

Zahid Ali
Zahid Ali

Reputation: 466

you need to put if inside while loop.

    String one = "";
    boolean yea = true;   
    Scanner sw = new Scanner(System.in);
    while (yea == true)
    {
        System.out.print(MENU);
        one  = sw.next();
        char choice  = one.charAt(0);
        switch(choice)
        {
            case 'f':
                friendsList();
                break;
            case 'w':
                wall();
                break;
            case 'p':
                network();
                break;

            case 'q' : 
                yea = false;
                break; 
            default:
                System.out.println("Error: You have entered " + choice + 
                ". Please try again");

        }
        if (choice == 'f')
        {
        break;
        }

    }

Upvotes: 0

Victor Johnson
Victor Johnson

Reputation: 456

For your implementation, it would make sense to break on a specific case before even entering the switch statement. For example:

char choice  = one.charAt(0);

if (choice == 'f') break;

switch(choice)

This seems to be a pretty simple way to exit the while loop without conflicting with the break statements of the switch statement.

Or if you still need to call the friendsList method when choice is 'f' you can move that if statement to after the switch statement.

Note: With this you should also remove the if statement at the bottom of your code example.

Upvotes: 1

doublesharp
doublesharp

Reputation: 27607

You would use a Java label (see this code example named BreakWithLabelDemo.java) to tell your code where to break.

myloop:
    while ( true ){
        switch( choice ){
            case 'f':
                friendsList();
                break myloop;
        }
    }

Upvotes: 1

Anuja
Anuja

Reputation: 124

if (case == 'f')

What is case in this statement? You should replace that with choice.

if (choice == 'f')

Upvotes: 0

Related Questions