Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning control to switch statement in java?

Tags:

java

loops

I have a client-server console based application, in client side I have used switch statement for selecting options such as upload/ download/ change password etc. When user enters one number for suppose

      String userchoice = console.readLine("Enter your choice :"); 
      int choice= Integer.parseInt(userchoice);
      switch (choice){  
      case 3: 
      ........
      Socket soc = new Socket("localhost", 6007);
      String reply;
      String client = username;
      char newpswd[] = console.readPassword("Enter your new Password :");
      String newpwd=new String(newpswd);
      char newpswd1[] = console.readPassword("Confirm your new Password :");
      String newpwd1=new String(newpswd1);
      if(newpwd.equals(newpwd1)) {
      ........
      }
      else {
      S.O.P ("Passwords don't match");  
      }
      break;

After the process has been finished, then I need to send the user to switch (choice) statement again asking for the option number to enter. I have tried continue, return but none worked for me. return - will return to JVM I suppose, which is exiting the program. As goto is not used in Java, what will be my alternative ?

like image 328
highlander141 Avatar asked Dec 01 '25 02:12

highlander141


2 Answers

After the process has been finished, then I need to send the user to switch (choice) statement again

Then you need a loop:

while (!quit) {
    String userchoice = console.readLine("Enter your choice :"); 
    ...
    switch (...) {
        ...
    }
}
like image 144
Jon Skeet Avatar answered Dec 02 '25 15:12

Jon Skeet


do {

...

}while(choice != EXIT_CHOICE);

where EXIT_CHOICE is a constant

like image 38
user902383 Avatar answered Dec 02 '25 14:12

user902383