JAVA
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: Mary had a little cat: there are 2 words of length three
code
public class MyClass {
public static void main(String args[]) {
int n=0;
if(args.length < 2)
throw new IllegalArgumentException("Less than 2 command line arguments given ");
else
{ for(int i = 0; i<args.length; i++) {
if(args[i].length() == 3)
{ n = n+1;}
}
System.out.println("there are "+ n + " words of length three");
}
}
}
screenshot
Get Answers For Free
Most questions answered within 1 hours.