Hey everybody, the program I am writing takes 10 digits as a floating point as a number then displays the average of the numbers and any numbers that are greater than it after it.
My program will compile but every time I run it through I get the error message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at
avggrt.main(avggrt.java:44)

import TerminalIO.KeyboardReader;
 public class avggrt
{
  public static void main (String [] args)
	{
	  KeyboardReader reader = new KeyboardReader();
	  double[] num = new double[10];
	  double[] pri = new double[10];
	  double[] grt = new double[10];
	  double number = 0.0;
	  double total = 0.0;
	  double rem = 0.0;
	  double ttotal = 0.0;
	  double avg = 0.0;
	  int cnum = 0;
	  int scnum = 0;
	  int fcnum = 0;

	  for (int i = 0;i < 10; i++)
	  {

	  	  System.out.print("Please Enter your Number: ");
	  	  number = reader.readDouble();
	  	  total = total + number;
		  num[cnum] = number;
		  cnum++;

	  	
 	  }

		avg = total / 10;
		rem = total % 10;
		ttotal = avg + rem;

			for(int j = 0; j < cnum; j++)
			{
			  if(num[scnum] > ttotal)
			  num[scnum] = grt[scnum];
			  scnum++;

			    {
				for(int a = 1; a < 11; a++) 
				  {
				  pri[fcnum] = grt[scnum];
				  fcnum++;
				  }
			

	}
}
}
}

Thanks in advance.

Recommended Answers

All 15 Replies

If your indentation is right then you increment fcnum inside 2 nested loops without ever resetting it, so its hardly surprising that it eventually gets >9 and is no longer a valid index for a 10 element array.

But I've tried switching my array to size 20 and I get the same error still

At a guess cnum is 10, so the inner loop's code will be executed 100 times and fcnum will reach 100. Since I don't know what the array pri is for, I can't advise on how to fix this. Meaningful variable names and comments explaining the goals will help a lot, I think.

Meaningful variable names and comments explaining the goals will help a lot, I think

Second that. Sit down and explain this program to someone, and every time you get to a variable, listen to how you explain it. That's the name of this variable. If you find yourself wondering why a particular variable exists, get rid of it.

cnum, for example, is used for two things: it tracks the index of the array of numbers entered by the user (which your loop counter, i, is already doing) and it serves to hold the index of the last number entered, which you know already is ( (num.length)-1) . This fact - that the number of numbers entered is always and forever 10 - is also enshrined in the hard-coded constant 10 used to calculate average and remainder.
Why is cnum here? All it does is obscure your code.


Then there's scnum - what's that for? There's never a time when it's referenced and it doesn't equal j.

Now since fcnum does the same thing as cnum and scnum (where do these names come from?), and your inner loop counts from 0 to 10, you should be able to see where your out of bounds is coming from.

cnum = count number
scnum = second count number
fcnum = final count number

array num is my array that the numbers are originally put in
array grt is my array that anything greater than the average goes in
array pri is my array everything is going to go into to and be displayed

I see what you are saying about my counts being useless, do you think it would be better to put my counter for my for loops in the beginning of the code instead of in my for loop?

The only reason you'd have to keep track of the number of items in the array would be if the user could enter up to N numbers, and you were using an array of N to store them. Since the user has to enter 10 numbers, use .length to know where the data ends. Use this for loops, and also for the count of items when you're calculating the average. This way, you're sure that every reference to the number of items is taken directly from the best source, and if you want to change the number of items entered, you need only change the size of num.

Now, when you're selecting certain numbers from the original array for display, you might want to copy them into a new array (call it maybe printArray instead of pri). Then you'd have to keep track of how many numbers go into that array, and your count strategy would make some sense.
Or else you could use an array of 10 booleans, and set them to true or false, depending on whether the number should be printed. You'd get a line like

if (print[n]) System.out.println (numbers[n]);

I'd find that easier to follow.

There's always a chance that the number 10 may change later (eg "part2 - see how your program responds for 1000 numbers"), so best practice is to declare a constant right at the start

static final int NO_OF_DATA_POINTS = 10;

then use that to dimension your arrays, control your loops etc.
1. It will make it more obvious what your loops etc are about
2. Its a single line to change for different data volumes.

So my code now looks like this

import TerminalIO.KeyboardReader;
 public class avggrt
{
  public static void main (String [] args)
	{
	  KeyboardReader reader = new KeyboardReader();
	  double[] num = new double[10];
	  double[] print = new double[10];
	  double[] grt = new double[10];
	  double number = 0.0;
	  double total = 0.0;
	  double rem = 0.0;
	  double ttotal = 0.0;
	  double avg = 0.0;
	  int cnum = 0;
	  int scnum = 0;
	  int fcnum = 0;

	  for (int i = 0;i < 10; i++)
	  {

	  	  System.out.print("Please Enter your Number: ");
	  	  number = reader.readDouble();
	  	  total = total + number;
		  num[i] = number;
		  //cnum++;

	  	
 	  }

		avg = total / 10;
		rem = total % 10;
		ttotal = avg + rem;

			for(int j = 0; j < 10; j++)
			{
			  if(num[j] > ttotal)
			  num[j] = grt[j];
			  //scnum++;

			    {
				for(int a = 1; a < 10; a++) 
				  {
				  print[a] = grt[j];
				 //fcnum++;
				  }
			

	}
}
}
}

Now it runs without error, but after I enter in my 10 numbers nothing happens. It does not display or anything it just ends.

It only does what you tell it to do! You haven't written any code to display anything.

Now my display prints the correct average, but it does not print the numbers that are greater, and it prints it over and over again.

for(int y = 1; y < 10; y++)
					  {
					System.out.print("The average is" +print[0]+ "the numbers that are greater are" +print[y]);
					  }

My question is would there be a way to make the index of the array go up without printing the answer over and over?

Well, what line does that execute over and over? What does that line do?
If you don't want a part of it to execute repeatedly, where should you put that part - or rather, where should you not put that part?

I don't want it to print the answer over and over, but I don't know how to get the array index to increase.

Take the part that you want to print just once out of the loop.

System.out.println ("Prints once");
for (int i = 0; i<Integer.MAX_INTEGER; i++)
{ 
  System.out.println("Prints many, many, many times, with counter. Count = "+i)
}

I am now getting the same error I was having at the very beginning from my first post
where it will compile and run, but after I run it the error pops up.

import TerminalIO.KeyboardReader;
 public class avggrt
{
  public static void main (String [] args)
	{
	  KeyboardReader reader = new KeyboardReader();
	  double[] num = new double[10];
	  double[] print = new double[10];
	  double[] grt = new double[10];
	  double number = 0.0;
	  double total = 0.0;
	  double rem = 0.0;
	  double ttotal = 0.0;
	  double avg = 0.0;
	  int cnum = 0;
	  int scnum = 0;
	  int fcnum = 0;
	  int y = 0;
	  int p = 0;

	  for (int i = 0;i < 10; i++)
	  {

	  	  System.out.print("Please Enter your Number: ");
	  	  number = reader.readDouble();
	  	  total = total + number;
		  num[i] = number;
		  //cnum++;

	  	
 	  }

		avg = total / 10;
		rem = total % 10;
		ttotal = avg + rem;
		print[0] = ttotal;

			for(int j = 0; j < 10; j++)
			{
			  if(num[j] > ttotal)
			  num[j] = grt[p];
			//  if(num[j] = grt[p])
			  
			  //scnum++;

			    {
				for(int a = 1; a < 9; a++) 
				  {
				  print[a] = grt[p];
				 //fcnum++;
				  p++;
				  }
			

	}
}					
					System.out.print("The average is" +print[0]+ "the numbers that are greater are" +grt[y]);
					y++;

}
}

... for exactly the same logic error (y++ inside nested loops)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.