Reputation: 71
First of all, I'm very new. So, I was able to get this to run when everything was int, but for some reason changing it to double, I can only input the first value and it errors. I don't understand why I can't do this:
int datVal = Integer.parseInt(inNum);
double [] iOne;
iOne = new double[datVal];
There is no error until I run it or debug it. Here's what I've got:
import java.util.Arrays;
import javax.swing.JOptionPane;
public class arStats2 {
public static void main(String[] args) {
double med, men, min, max;
String inNum = JOptionPane.showInputDialog("Enter the number of data values:");
int datVal = Integer.parseInt(inNum);
double [] iOne;
iOne = new double[datVal];
double [] iTwo;
iTwo = new double[datVal];
int index=0;
for (index=0; index < iOne.length; index++) {
String value = JOptionPane.showInputDialog("Input a data value:");
int valIn = Integer.parseInt(value);
iOne[index] = valIn;
}
System.arraycopy(iOne, 0, iTwo, 0, iOne.length);
Arrays.sort(iOne);
String out1 = "Sorted data: ";
for (int i=0; i<iOne.length; i++) {
out1 = out1 +iOne[i]+"";
}
out1 = out1 +"\n";
String out2 = "Original data: ";
for (int i2=0; i2<iTwo.length; i2++) {
out2 = out2 +iTwo[i2] + "";
}
out2 = out2 +"\n";
med = median(iOne);
men = mean(iTwo);
max = computeMax(iOne);
min = computeMin(iOne);
JOptionPane.showMessageDialog(null, out2 +out1 +"Min Value: " +min +"\n"
+"Max value: " +max +"\n"
+"Median value: " +med +"\n"
+"Mean value: " +men +"\n");
}
public static double median(double[] iOne) {
double med;
int index, indexHi, indexLo;
if ((iOne.length %2) !=0) {
index = iOne.length / 2;
med = iOne[index];
}
else {
indexHi = iOne.length / 2;
indexLo = indexHi = 1;
med = (iOne[indexLo] + iOne[indexHi])/2;
}
return med;
}
public static double mean(double[] iOne) {
double sum = 0;
for (int i = 0; i < iOne.length; i++) {
sum += iOne[i];
}
return sum / iOne.length;
}
public static double computeMax(double[] iOne) {
double max = (iOne.length - 1);
return max;
}
public static double computeMin(double[] iOne) {
double min;
min = iOne[0];
return min;
}
}
The error is:
Exception in thread "main" java.lang.NumberFormatException: For input string: "7.2"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at arStats2.main(arStats2.java:25)
Upvotes: 0
Views: 185
Reputation: 89
but for some reason changing it to double, I can only input the first value and it errors.
Well the reason is datVal = Integer.parseInt(inNum);
expects an Integer and you are passing it a string that contains double which will result in a NumberFormatException being thrown.
you should surround this with a try{} catch {}
block and do something about that.
Upvotes: -1
Reputation: 6308
It would be helpful if you could be more specific about the error. However, there are two parts of the code that I believe to be wrong:
indexLo = indexHi = 1;
should be
indexLo = indexHi - 1;
And in computeMax()
you should have:
double max = iOne[iOne.length - 1];
Upvotes: 0
Reputation: 98
You need to parse the values as double here:
for (index=0; index < iOne.length; index++) {
String value = JOptionPane.showInputDialog("Input a data value:");
double valIn = Double.parseDouble(value); // this line is changed
iOne[index] = valIn;
}
This will make it work with double values, but there are also other problems in your code, as pointed out by ValarDohaeris.
Upvotes: 2