-3

I'm creating a program to find the standard deviation of an ArrayList of double values given by the user. I am to provide a string on the command line to indicate whether I want the mean (mean), or the standard deviation (std). However, I am having difficulty giving those strings to the command line.

This is the error I'm getting:

> Exception in thread "main" java.lang.NumberFormatException: For input string: "std"
        at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.base/java.lang.Integer.parseInt(Unknown Source)
        at java.base/java.lang.Integer.parseInt(Unknown Source)
        at StatDriver.main(StatDriver.java:28)

        public static void main(String[] args) {

          Scanner input;
          ArrayList<Double> data;
          int dataSize;
          String userInput = "";

          // Determine the appropriate array size.
          if (args.length > 0) {
             dataSize = Integer.parseInt(args[0]);
          } else {
             dataSize = 15;
          }

          // Create the array.
          data = new ArrayList<Double>(dataSize);

          // Read values from the terminal.
          input = new Scanner(System.in);
          while (input.hasNextDouble()) {
             data.add(input.nextDouble());
          }

          // Calculate and display the results.
          if(args[0] == "mean") {
              System.out.printf("Mean: %.2f\n", Stats.mean(data));
          }
          else if (args[0] == "std") {
              System.out.printf("StdDev: %.2f\n", Stats.stdDev(data));
          }
          else {
              System.out.printf("Mean: %.2f\n", Stats.mean(data));      
         }
       }
    }

1 Answers1

0

This needs to be fixed:

If you are passing in a single command line argument "std" or "mean", then since args.length is greater than 0, an error will be thrown trying to parse "std" or "mean" to an Integer value.

      // Determine the appropriate array size.
      if (args.length > 0) {
         dataSize = Integer.parseInt(args[0]);
      } else {
         dataSize = 15;
      }

So if you change your check to something like this, and input "java StatDriver std 10":

The parse will succeed, as you will be able to parse the argument "10" at the second position.

  // Determine the appropriate array size.
  if (args.length > 0) {
     dataSize = Integer.parseInt(args[1]);
  } else {
     dataSize = 15;
  }

Generally if you run a program java program with command line arguments:

java MyProgram one two

public static void main(String [] args) {
    String one = args[0]; //=="one"
    String two = args[1]; //=="two"
}
Jesse
  • 1,814
  • 1
  • 21
  • 25
  • I should be able to provide a string on the command line indicating whether they want to calculate the mean ("mean") or the standard deviation ("std") so I'm assuming that dataSize should be a statement for a string value? – Evan Ross Davis Jul 02 '18 at 03:51
  • Correct you can provide multiple command line arguments. I would treat them separately. Your problem right now is you are doing a check to determine whether the args.length > 0 and trying to parse the arg at position 0 to an int. In your case you are inputting a string at position 0 ("std" or "mean"), and that is causing your program to crash because you cannot parse "std" or "mean" to an Integer. – Jesse Jul 02 '18 at 03:53
  • I'm not asking how to compare Strings here. If I was, I could have debugged this myself. – Evan Ross Davis Jul 02 '18 at 04:23
  • The problem is not the command line arguments. It is trying to parse a string to an int – Jesse Jul 02 '18 at 04:32
  • I still don't understand what you're saying. I guess I'll let an Indian teach me on YouTube. I'm not just going to start adding/subtracting stuff when I don't know what it means. I'll leave that to the "professionals." – Evan Ross Davis Jul 02 '18 at 04:35
  • okay so when you execute the command "java StatDriver std", args[0] is "std". You cannot execute dataSize = Integer.parseInt(args[0]), which is now dataSize = Integer.parseInt("std") Because "std" cannot be parsed to an Int. That is why you are getting the error. There is nothing wrong with the way you are handling command line arguments. Change that line and your program will work. – Jesse Jul 02 '18 at 04:41
  • I'll be working on this all night so give me a sec to respond. – Evan Ross Davis Jul 02 '18 at 04:43