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

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 length three. (Hint: loop through the args array) If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message. For ex: Jenna has a new sister: there are 2 words of length three public class Question1 { public...
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...
Write a method weave that takes two arrays of ints, a and b, and that returns...
Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array. In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT