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 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 static void main (String args[]) {

Solutions

Expert Solution

public class StringsOfLength3 {
  
public static void main(String[] args) {
if(args.length < 2)
throw new IllegalArgumentException("There should be atleast 2 command-line arguments!");
  
// run a loop over the args[] array and print the strings of length 3
String result = "";
for(int i = 0; i < args.length; i++)
{
if(args[i].length() == 3)
result += args[i] + " ";
}
  
// if the result is empty, print an appropriate message
if(result.equals(""))
System.out.println("There are no words of length 3!");
else
System.out.println("The words of length 3 are: " + result);
}
}

****************************************************** SCREENSHOT **********************************************************

COMMAND-LINE ARGUMENT :

CONSOLE 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.
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