Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the java code below, I cannot get the menu to loop until the user exits the p

ID: 665821 • Letter: I

Question

In the java code below, I cannot get the menu to loop until the user exits the program.

import java.util.Scanner;

class VowelsConsonants
{
private static String sentence;
public VowelsConsonants(String str)
{
sentence = str;
}
public static int vowelCount()
{
int count = 0;
for(int i = 0; i<sentence.length();i++)
{
if(sentence.charAt(i)=='a'||
sentence.charAt(i)=='A'|| sentence.charAt(i)=='e'
||sentence.charAt(i)=='E'|| sentence.charAt(i)=='i'
||sentence.charAt(i)=='I'|| sentence.charAt(i)=='o'
||sentence.charAt(i)=='O'|| sentence.charAt(i)=='u'
||sentence.charAt(i)=='U')
{
count++;
}
  
}
return count;
}
  
public static int consonantCount()
{
int count = 0;
for(int i=0; i<sentence.length();i++)
{
if(sentence.charAt(i)!='a'&&
sentence.charAt(i)!='A'&& sentence.charAt(i)!='e'
&&sentence.charAt(i)!='E'&& sentence.charAt(i)!='i'
&&sentence.charAt(i)!='I'&& sentence.charAt(i)!='o'
&&sentence.charAt(i)!='O'&& sentence.charAt(i)!='u'
&&sentence.charAt(i)!='U')
{
count++;
}
}
return count;
}
}
  
public class TestClass
{
public static void main(String[] args)
{
String str, choice;
char ch;

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter string:");
str = keyboard.nextLine();

VowelsConsonants object = new VowelsConsonants(str);

do
{
System.out.println("1. Count Vowels");
System.out.println("2. Count Consonants");
System.out.println("3. Count both Vowels and Consonants");
System.out.println("4. Enter another String");
System.out.println("5. Exit Program");

System.out.println("Enter choice");
choice = keyboard.nextLine();
ch = choice.charAt(0);

switch(ch)
{
case '1':
System.out.println("Number of Vowels is:"+object.vowelCount());
break;
case '2':
System.out.println("Number of Consonants is:"+object.consonantCount());
break;
case '3':
System.out.println("Number of Vowels is:"+object.vowelCount());
System.out.println("Number of Consonants is:"+object.consonantCount());
break;
case '4':
System.out.print("Enter string:");
break;
case '5':
System.exit(0);
  
}
}while(ch=='4');

System.exit(0);
}
}

Explanation / Answer

//VowelsConsonants.java
public class VowelsConsonants
{
   private String sentence;
   public VowelsConsonants(String str)
   {
       sentence = str;
   }
   //Returs the number of vowles in the string
   public int vowelCount()
   {
       int count = 0;
       for(int i = 0; i<sentence.length();i++)
       {
           if(sentence.charAt(i)=='a'||
                   sentence.charAt(i)=='A'|| sentence.charAt(i)=='e'
                   ||sentence.charAt(i)=='E'|| sentence.charAt(i)=='i'
                   ||sentence.charAt(i)=='I'|| sentence.charAt(i)=='o'
                   ||sentence.charAt(i)=='O'|| sentence.charAt(i)=='u'
                   ||sentence.charAt(i)=='U')
           {
               count++;
           }

       }
       return count;
   }
   //Returs the number of consonants in the string
   public int consonantCount()
   {
       int count = 0;
       for(int i=0; i<sentence.length();i++)
       {
           if(sentence.charAt(i)!='a'&&
                   sentence.charAt(i)!='A'&& sentence.charAt(i)!='e'
                   &&sentence.charAt(i)!='E'&& sentence.charAt(i)!='i'
                   &&sentence.charAt(i)!='I'&& sentence.charAt(i)!='o'
                   &&sentence.charAt(i)!='O'&& sentence.charAt(i)!='u'
                   &&sentence.charAt(i)!='U')
           {
               count++;
           }
       }
       return count;
   }
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**The java program that allows user to select a menu choice and display the result . The program continues
* until user enters option 5 to exit.*/


//TestClass.java
import java.util.Scanner;
public class TestClass
{
     public static void main(String[] args)
     {
         String str, choice;
         char ch;
       
         Scanner keyboard = new Scanner(System.in);
         System.out.print("Enter string:");
         //read string from user
         str = keyboard.nextLine();
         //Create an instance of the class VowelsConsonants with string argument
         VowelsConsonants object = new VowelsConsonants(str);
       
         do
         {
             System.out.println("1. Count Vowels");
             System.out.println("2. Count Consonants");
             System.out.println("3. Count both Vowels and Consonants");
             System.out.println("4. Enter another String");
             System.out.println("5. Exit Program");
         
             System.out.println("Enter choice");
             choice = keyboard.nextLine();
             ch = choice.charAt(0);
           
             switch(ch)
             {
                 case '1':
                     System.out.println("Number of Vowels is:"+object.vowelCount());
                     break;
                 case '2':
                     System.out.println("Number of Consonants is:"+object.consonantCount());
                     break;
                 case '3':
                     System.out.println("Number of Vowels is:"+object.vowelCount());
                     System.out.println("Number of Consonants is:"+object.consonantCount());
                     break;
                 case '4':
                  
                   //read string from user
                   System.out.print("Enter string:");
                     str = keyboard.nextLine();
                     //create an an instance of the class VowelsConsonants
                     //and set str as the argument to the constructor of the class
                     object = new VowelsConsonants(str);
                     break;
                 case '5':
                   //exit from the program
                     System.exit(0);      
             }
          
            
         }while(true);
         //set while loop test condtion as true it continues until user enters
         // 5 to exit from the program
       
       
     }//end of main
}//end of the class

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

Enter string:aeiou
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
1
Number of Vowels is:5
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
2
Number of Consonants is:0
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
3
Number of Vowels is:5
Number of Consonants is:0
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
4
Enter string:sunday
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
1
Number of Vowels is:2
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
2
Number of Consonants is:4
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
3
Number of Vowels is:2
Number of Consonants is:4
1. Count Vowels
2. Count Consonants
3. Count both Vowels and Consonants
4. Enter another String
5. Exit Program
Enter choice
5

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote