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));
}
}
}