Write a complete main method that does the following Takes 1

Write a complete main method that does the following: Takes 1 command line argument and prints to the console the number of times two letter \"e\" s appear next to each other. If there is not at least command line argument, throw an IllegalArgumentException with an appropriate message. For example: e: > Java Question 1 \"The bees are needing a keeper.\" The string contains \"ee\" 3 times.

Solution

Hi Please find my code.

public class StringTest {

  

  

   public static void main(String[] args) {

      

       // if there is no command line arguments, throw exception

       if(args == null){

           throw new IllegalArgumentException(\"Please pass at least one command line argument\");

       }

      

       int length = args[0].length(); // length of first command line argument

       String str = args[0];

      

       int count = 0; // to keep occurence of \"ee\"

      

       int i=0;

       while(i < length){

          

           if(str.charAt(i) == \'e\'){ // if current character is \'e

               if((i+1) < length && str.charAt(i+1) == \'e\') // if next character is also \'e\',then increment count

                   count++;

                   i++;

           }

          

           i++;

       }

      

       System.out.println(\"The \\\"\"+str+\"\\\" contains \'ee\' \"+count+\" times\");

      

   }

}

/*

Sample Output:

The \"The bees are needing keeper\" contains \'ee\' 3 times

*/

Run from command line:

StringTest \"The bees are needing keeper\"

 Write a complete main method that does the following: Takes 1 command line argument and prints to the console the number of times two letter \
 Write a complete main method that does the following: Takes 1 command line argument and prints to the console the number of times two letter \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site