I've recently come upon a frustrating Array Index Out Of Bounds error when selecting any of the "calculations" to be applied to the entered values(maximum 1000 values). I suspect it to be a problem with the
values[(c+1)]
in the for loop.
I can't figure a way around this problem. Can someone offer up a tidbit? The code is below.
Scanner keyboard = new Scanner(System.in);
double[] values;
values = new double[1000]; //create a 1000 slot array
int count;
//int h1,h2,h3,h4,h5,h6,h7,h8,h9,h10;
for(count = 0; count <= values.length; count++){
System.out.println("Enter a value (-1000 to quit input): ");
values[count] = keyboard.nextDouble();
if (values[count] == -1000){
//start calculations
System.out.println("1. Mean");
System.out.println("2. Variance and Standard Deviation");
System.out.println("3. Histogram");
System.out.println("4. Exit");
int switchvar = keyboard.nextInt();
double meantrans = 0;
switch (switchvar){
case 1:
System.out.println("Mean: ");
int c;
for(c = 0; c <= values.length; c++){
meantrans = values[c] + values[(c+1)];
}
double mean = (meantrans / c);
System.out.println("The mean = " + mean);
break;
case 2:
System.out.println("Variance and Standard Deviation: ");
for(c = 0; c <= values.length; c++){
meantrans = values[c] + values[(c+1)];
} //mean calc
mean = (meantrans / c);
double vartrans = 0;
for(c = 0; c <= values.length; c++){
vartrans = values[c]+values[(c+1)];
}
double variance = vartrans / (c - (java.lang.Math.pow(mean,2)));
System.out.println("The variance = " + variance);
break;
case 3:
int h1=0,h2=0,h3=0,h4=0,h5=0,h6=0,h7=0,h8=0,h9=0,h10=0;
System.out.println("Histogram: ");
for(c = 0; c <= values.length; c++){
if(values[c] > 0.0 && values[c] < 1.0){
h1++;
}
else if(values[c] > 1.0 && values[c] < 2.0){
h2++;
}
else if(values[c] > 2.0 && values[c] < 3.0){
h3++;
}
else if(values[c] > 3.0 && values[c] < 4.0){
h4++;
}
else if(values[c] > 4.0 && values[c] < 5.0){
h5++;
}
else if(values[c] > 5.0 && values[c] < 6.0){
h6++;
}
else if(values[c] > 6.0 && values[c] < 7.0){
h7++;
}
else if(values[c] > 7.0 && values[c] < 8.0){
h8++;
}
else if(values[c] > 8.0 && values[c] < 9.0){
h9++;
}
else if(values[c] > 9.0 && values[c] < 10.0){
h10++;
}
}//end for
String ast1 = "";
for(int h=0; h <= h1; h++){
ast1 = ast1 + "*";
}
String ast2 = "";
for(int h=0; h<=h2;h++){
ast2 = ast2 + "*";
}
String ast3 = "";
for(int h=0; h<=h3;h++){
ast3 = ast3 + "*";
}
String ast4 = "";
for(int h=0; h<=h4;h++){
ast4 = ast4 + "*";
}
String ast5 = "";
for(int h=0; h<=h5;h++){
ast5 = ast5 + "*";
}
String ast6 = "";
for(int h=0; h<=h6;h++){
ast6 = ast6 + "*";
}
String ast7 = "";
for(int h=0; h<=h7;h++){
ast7 = ast7 + "*";
}
String ast8 = "";
for(int h=0; h<=h8;h++){
ast8 = ast8 + "*";
}
String ast9 = "";
for(int h=0; h<=h9;h++){
ast9 = ast9 + "*";
}
String ast10 = "";
for(int h=0; h<=h10;h++){
ast10 = ast10 + "*";
}
//output sequence
System.out.println("[0.0 - 1.0] | " + ast1);
System.out.println("(1.0 - 2.0] | " + ast2);
System.out.println("(2.0 - 3.0] | " + ast3);
System.out.println("(3.0 - 4.0] | " + ast4);
System.out.println("(4.0 - 5.0] | " + ast5);
System.out.println("(5.0 - 6.0] | " + ast6);
System.out.println("(6.0 - 7.0] | " + ast7);
System.out.println("(7.0 - 8.0] | " + ast8);
System.out.println("(8.0 - 9.0] | " + ast9);
System.out.println("(9.0 - 10.0] | " + ast10);
break;
case 4:
return;
}//end switch
}//end if
}//end for