Answer:
// name of the package
package stringlength;
// Scanner is imported but it is not needed
// So it is commented
// import java.util.Scanner;
// The class name StringLength is defined
public class StringLength {
// main method to signify the beginning of program execution
public static void main(String[] args) {
// if-statement to check if argument is supplied to
// command line
if(args.length > -1) {
// the first argument is read and assigned to input
String input = args[0];
int length = input.length(); // using length() method in String class
System.out.println("Your string has a length of "+length+" characters.");
} else {
System.out.println("Please enter your string argument in quote");
}
}
}
Explanation:
The code works fine with little modification and comments. The user should supply the string arguments in quote. The quote must surround the strings because our logic read the first argument passed to the command line. Inserting a string with space between them will provide a wrong output.
A sample image is attached.