In: Computer Science
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[]) {
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 :