Question

In: Computer Science

Write a complete main method that does the following: Takes any number, but at least two,...

Write a complete main method that does the following: Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array)

If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.

Solutions

Expert Solution

Please find the code below:


public class CLA {
        public static void main(String[] args) throws IllegalArgumentException{
                if(args.length < 2) //if less than 2 words entered
                {
                        throw new IllegalArgumentException("Enter more than 2 args"); //throw exception
                }
                else
                {
                        int count = 0; //count of words
                        for(String s : args) //iterate through args
                        {
                                if(Character.isDigit(s.charAt(s.length() - 1))) //if character at last index is a digit
                                {
                                        count++; //increment count
                                }
                        }
                        System.out.println("Number of words that end with a number are : "+count); //display count
                }
        }
}

Supplied Input:

Corresponding Output:

Number of words that end with a number are : 3

Thanks, Please upvote if you appreciate the effort. :)


Related Solutions

In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Write a method that takes the sides of a dice and produces a random number between...
Write a method that takes the sides of a dice and produces a random number between 1 and the value of the dice sides. use the following header: public static int roll(int diceSides) Add a main method to the class that tests the method by prompting for a int for dice sides and a color for the die, invoking the method and printing the returned number and the color of the die. SAMPLE RUN #1: java DiceRoll Enter a number...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
1) Write a method that takes a string representing a positive binary number, and writes out...
1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.) 2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters. 3) Write an encodeString method that encodes a string of ASCII Characters. Input: It takes two parameters: a) inputString:...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT