Issue
sorry if the title is a tad confusing. I've been working on this all day so my brain is a bit fried. What I want to do is allow the user to continue to receive the menu options until “Exit” is chosen, but only one string is entered; as well as if an invalid menu selection is made, then the program would report such to the user and offer the menu options again. I'm just very lost on what to do and would very much appreciate any help.
public class McLemorePProg5 {
public static void main(String[] args) {
//Create scanner
java.util.Scanner mango = new java.util.Scanner(System.in);
//Print Statements
//User is requested to enter a string
System.out.printf("Please enter a string:\n");
//User enters a string
String s = mango.nextLine();
//Operation menu is displayed
System.out.println("(1) Report the number of vowels in the string (a, e, i, o, u, y)");
System.out.println("(2) Report the number of consonants in the string");
System.out.println("(3) Report the number of lower case letters in the string");
System.out.println("(4) Print out the characters of the strings that are in even positions in the string");
System.out.println("(5) Exit program ");
System.out.printf("\nPlease enter an operation number: (1), (2), (3), (4) or (5): \n");
//User enters an operation
int op = mango.nextInt();
switch (op) {
case 1:
System.out.println("The number of vowels in the string is " + countVowels(s));
break;
case 2:
System.out.println("The number of consonants is " + countConsonants(s));
break;
case 3:
System.out.println("The number of lowercase letters in the string is " + countLowercase(s));
break;
case 4:
System.out.println("the characters in even positions are:"); printEvenChars(s);
break;
case 5:
System.out.println("Program ended"); System.exit(1);
default:
System.out.println("Invalid operation choice. Please choose again");
}//end switch
//Close scanner
mango.close();
}//end main
//Methods
public static int countVowels(String s) { //Calculate the number of vowels
int vowelCount = 0;
for ( int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A' || s.charAt(i) == 'a' || s.charAt(i) == 'E' || s.charAt(i) == 'e' ||
s.charAt(i) == 'I' || s.charAt(i) == 'i' || s.charAt(i) == 'O' || s.charAt(i) == 'o' ||
s.charAt(i) == 'U' || s.charAt(i) == 'u' || s.charAt(i) == 'Y' || s.charAt(i) == 'y') {
vowelCount++; } //end if
}//end for loop
return vowelCount;
}//end countVowels
public static int countConsonants(String s) { //Calculate the number of consonants
int consonantCount = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A' || s.charAt(i) == 'a' || s.charAt(i) == 'E' || s.charAt(i) == 'e' ||
s.charAt(i) == 'I' || s.charAt(i) == 'i' || s.charAt(i) == 'O' || s.charAt(i) == 'o' ||
s.charAt(i) == 'U' || s.charAt(i) == 'u' || s.charAt(i) == 'Y' || s.charAt(i) == 'y') {
} //end if
else {
consonantCount++;
}//end else
}//end for loop
return consonantCount;
}//end countConsonants
public static int countLowercase(String s) { //Calculate the number of lower case letters
int lowercaseCount = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
lowercaseCount++; }//end if statement
}//end for loop
return lowercaseCount;
}//end countLowercase
public static void printEvenChars(String s) { //Prints the characters in even positions in the string
String evenChars = "";
for (int i = 0; i < s.length(); i++) {
if(i % 2 == 0) {
evenChars += s.charAt(i); }//end if statement
}//end for loop
System.out.println(evenChars);
}//end printEvenChars
}//end class
Solution
Don't resort to System.exit()
unless absolutely must. Here's the outline of what you should do instead:
int op = mango.nextInt();
while (op != 5) {
switch(op) {
// your switch cases
case 5:
// just do nothing
break;
}
// once you've processed that command, read in the next one.
// If it's 5, the loop will exit
// you may want to also print the menu here, so it appears after each command
op = mango.nextInt();
}
mango.close();
Answered By - iluxa
Answer Checked By - Katrina (JavaFixing Volunteer)