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 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...
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...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT