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
for(count = 0; count <= values.length; count++)

If you use count as an index into values in this loop, it will take you out of bounds, since the last index in values is (values-1). Using (count+1) as the index is just icing on the cake - it puts you out of bounds one iteration sooner.
The standard idiom for looping over all elements of an array is

for(count = 0; count [B]<[/B] values.length; count++)

Edited 5 Years Ago by jon.kiparsky: n/a

Notes on structure: never put your working code in the cases of your switch. Sub it out to methods, with nicely descriptive names.

case 1: calculateMean();
case 2: calculateVarianceAndStandardDeviation();
case 3: ...

You want to have yourself describing one level of logic at a time. If you're reading a cookbook, and it says "Take two onions. Check to see if the knife is sharp enough. If not, get out your whetstone. Lubricate your whetstone with the oil provided, or vegetable oil or WD-40. Holding the whetstone in your off hand, pull the knife from hilt to tip along the blade, with the blade at a 15 degree angle to the stone. Repeat two or three times. Turn your wrist to present to opposite side of the blade to the whetstones and again draw the knife from hilt to tip along the stone three times. Repeat, alternating sides, until the blade is sharp. Cut the onions in half and remove the outer peel...

At that point you've forgotten what you're cooking. Same with your program: with all that detailed logic in the case statement, it's hard to keep track of what it is we're actually trying to do.
Push the implementation details down into methods, and don't be afraid to sub out smaller parts of those into helper methods.

This question has already been answered. Start a new discussion instead.