CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2022
    Location
    Urbana, Illinios
    Posts
    14

    In Java, how do you return control to a switch statement?

    I have a client-server console program in which I utilize switch statements to pick choices such as upload/download/change password, among others. When a user inputs a single number for example,
    Code:
       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 procedure is completed, I must return the user to the switch (choice) statement and request the option number. I tried to continue and return, but neither worked for me. return - I believe this will return to the JVM, which is leaving the application. What will my alternative be because goto is not utilized in Java? I used the loop as said but it didn't work can someone help me out?
    Code:
    while (!quit) {
        String userchoice = console.readLine("Enter your choice :"); 
        ...
        switch (...) {
            ...
        }
    }
    Last edited by 2kaud; December 17th, 2022 at 11:25 AM. Reason: Web site reference removed

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: In Java, how do you return control to a switch statement?

    Quote Originally Posted by Nathan D View Post
    I tried to continue and return, but neither worked for me. return - I believe this will return to the JVM, which is leaving the application. What will my alternative be because goto is not utilized in Java? I used the loop as said but it didn't work can someone help me out?
    Code:
    while (!quit) {
        String userchoice = console.readLine("Enter your choice :"); 
        ...
        switch (...) {
            ...
        }
    }
    Where and how did you try "to continue and return"?
    Last edited by 2kaud; December 17th, 2022 at 11:25 AM.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured