Question

In: Computer Science

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.

Solutions

Expert Solution

Here is the Java code to given question.

Sample outputs are added at the end.

Code:

public class CommandLineTest {

    public static void main(String[] args){

        try{
            if(args.length<2){    /*if number of words in command line arguments is less than 2*/

                throw new IllegalArgumentException();    /*throws new IllegelArgumentException*/

            }
            else{

                int numberOfWords=0;     /*uswed to store number of words that end with a digit*/

                String currentWord;     /*used to store current word of command line argument*/

                for(int i=0;i<args.length;i++){     /*runs through all command line arguments*/

                    currentWord=args[i];     /*stores current word in a variable*/

                    int lastIndex=currentWord.length()-1;      /*computes the last index of currentWord*/

                    if(Character.isDigit(currentWord.charAt(lastIndex))){     /*checks if the current word ends with a digit*/

                        numberOfWords+=1;     /*increments the number of words by 1*/

                    }
                }

                System.out.println("Number of words that end with a digit: "+numberOfWords);    /*prints the number of words that end with a digit*/

            }

        }catch (IllegalArgumentException e){

            System.out.println(e+":  There should be atleast 2 command line arguments.");   /*prints exception with appropriate message*/

        }
    }
}

Sample output-1:

Command line arguments:

Output:

Sample output-2:

Command line arguments:

Output:


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 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.
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...
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...
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears).. Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string. countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 Write a method copies that, when given a string and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT