Question

JAVA Write a complete main method that does the following: Takes any number, but at least...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
We have recently been discussing Java arrays, and in particular, the use of the args[] array...
We have recently been discussing Java arrays, and in particular, the use of the args[] array in the main() method for parsing command-line arguments. For this portion of Lab #3, write a simple calculator program which reads a mathematical expression from the command line in the form of three arguments: one mathematical operator (either addition, subtraction, multiplication, or division), and two numeric operands. The program should then evaluate the given expression, converting the numeric operands to floating-point numbers (use the...
You shall write a Java program that functions similarly to the GNU strings utility. Your program...
You shall write a Java program that functions similarly to the GNU strings utility. Your program must: 1.Accept any number of command-line arguments, assumed to be file paths. -If the first command-line argument can be parsed as an integer, this integer shall represent the minimum sequence length. Otherwise, the minimum sequence length shall be 4 strings. 2.Print, one per line, to standard output, all printable character sequences that are at least as long as the minimum sequence length and are...
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int...
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int array and an int value. The method should return true if the value is found in the array, or false if the value is not found in the array. Demonstrate the method in a program that takes input from the user. First, the program should prompt the user to enter the length of the array. Then, it should prompt the user for every member...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
Here is the code I am supposed to Analyze: // If we want to use the...
Here is the code I am supposed to Analyze: // If we want to use the Scanner class (type) to get user input, we need // to import the following Java package that includes the Scanner class // definitions. We do not have to add an import statement to use the // print() or println() methods of the System object, because they // are built in. import java.util.Scanner; public class PRG420Week1_AnalyzeAssignment { /* The main() method you see below is...
Write a complete Java application according to the following specifications: The application is built with Ant...
Write a complete Java application according to the following specifications: The application is built with Ant and uses gson-2.8.5.jar (or later), which must be included properly in the NetBeans-exported zip file you submit to Canvas The application has at least the following four source-code files: StudentInfoGsonApp.java, Student.java, StudentList.java, PhoneNumber.java The main method in StudentInfoGsonApp creates the studentInfo String array as follows: String[] studentInfo = {"John, Doe, 3.1, 866-555-1212, Rust;Julia", "Jane, Deere, 3.25, 898-555-1212, swimming;sleeping;dreaming;kayaking", "Sam, Spade, 2.9, 888-555-1212, coffee-drinking;Java;Python"}; creates...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT