Bool inside switch

Joined
Jun 14, 2021
Messages
5
Reaction score
0
I wish to add a Boolean value as a control which determines if a different error message is displayed within my case statements, knowing I should declare the variable as:

Code:
boolean bool = true;

Inside my statement:

Code:
switch (data) {
                case "HELLO":
                    sendln(String.format("HELLO %s; pleased to meet you!", getSocket().getRemoteSocketAddress().toString()));
                    //set bool to false
                    break;
                case "BYE":
                    sendln("Brew what?");
                    break;
                default:
                    sendln("ERR Sorry did not understand. Say BYE if you wish to exit.");
                    break;
            }

Although the purpose of changing Boolean value from true-false is that I wish to display the message 'Already in session.' if the first case has previously been ran and I do not know how to code this.
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
Java:
public class Scratchpad {
  public static void main(String[] args) {

    String testVariable = "test";
    Boolean myToggle = false;

    switch(testVariable){
      case "nope":
        System.out.println("not a match");
        break;
      case "test":
        System.out.println("test variable equalled 'test'");
        myToggle = true;
        break;
      default:
        System.out.println("default case");
    }

    System.out.println(
      String.format("my toggle is: %s", myToggle)
    );

  } // main
} // Scratchpad
output said:
test variable equalled 'test'
my toggle is: true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top