-
October 20th, 2022, 04:33 AM
#1
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
-
October 20th, 2022, 09:00 AM
#2
Re: In Java, how do you return control to a switch statement?
Originally Posted by Nathan D
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|