0

For an assignment of mine, I have to print out the contents of the following arrays. Every time I try to run the program, it gets through the first part and displays only Mercury's information. Then I get an error of java.lang.ArrayIndexOutOfBoundsException: 1. I know this error usually means that I am trying to call on an index that does not exist in the array, but I do not know how to fix it.

public static void printResults(String[] name, double[] radius, double[] mass, double calcGravity[])
{
    for (int i = 0; i < radius.length; i++)
    {
        System.out.printf("%7s %12.1f %12.1e % 12.1f", name[i], radius[i], mass[i], calcGravity[i]);
    }
}
Emz
  • 1,280
  • 1
  • 14
  • 26
S.Bean
  • 1
  • 2
  • 3
    Look at each of the arrays you're passing. At least one of them only has a single member. – Andy Thomas Nov 25 '15 at 18:21
  • 3
    Make sure all your array have the same length – Guillaume F. Nov 25 '15 at 18:22
  • one or more of the other arrays have a `length` that's less than `radius.length`. – Igwe Kalu Nov 25 '15 at 18:22
  • 2
    Note that this code is screaming for objects: instead of passing 4 arrays of strings and doubles, you should pass a single array of Planets, where each Planet would have a field name, a field radius, a field mass and a field gravity. That would also have the nice side effect of removing your bug. – JB Nizet Nov 25 '15 at 18:23
  • Usually this type of error is because your code tried to access element index 1 of an array that has less than 2 elements. – makertech81 Nov 25 '15 at 18:24
  • @JBNizet, well that's not the point of the question. First address the primary point and then offer you may offer improvements once that's done. – Igwe Kalu Nov 25 '15 at 18:24
  • @IgweKalu if I adressed the point of the question, it would be an answer. But it's not an answer, it's a comment. That's what they're for. I know quite well how StackOverflow works, thank you. – JB Nizet Nov 25 '15 at 18:25
  • you may determine the smallest length first then modify your loop as follows: `for (int i = 0; i < smallessLength; i++){...}` – Igwe Kalu Nov 25 '15 at 18:26
  • 3
    @IgweKalu this is terrible advice: it would replace a well-visible bug by a more subtle, harder to find one: all the planets wouldn't be printed. The bug is in the calling code, not in the posted code. – JB Nizet Nov 25 '15 at 18:28
  • Thank you too @JBNizet, I just thought it's not thoughtful to go off point when OP's problem remain unsolved. – Igwe Kalu Nov 25 '15 at 18:28
  • @JBNizet, all the terrible advices so far could help the OP to understand what's wrong. And since the OP knows the context of the problem more than anyone, s/he should then be able to reach a better solution.. If I were actually offering an exact solution I would post it in answer. Meanwhile, do you have a better or less terrible advice? - Do share please so that I can as well learn. – Igwe Kalu Nov 25 '15 at 18:33
  • My better advice was to use a Planet class, and a single array of Planet objects. I see no point in repeating everything that is already said in the previous comments and answers. I never said that all the other comments are terrible advices. Only yours. – JB Nizet Nov 25 '15 at 18:36

2 Answers2

0

First of all, ArrayIndexOutOfBoundException is for accessing array by illegal index (which basically means index >= array.size or index < 0). I am not going to write any code, since this is a task for you to learn something, but pay attention to sizes of each array and make sure they are of equal size.

It should be pretty easy to figure out.

Iliiaz Akhmedov
  • 827
  • 7
  • 17
0

Look at each of the arrays being passed to the method. At least one of them only has a single member.

Then determine why the caller is not passing the parallel arrays that your method expects.

One way to examine the arrays is in a debugger. You can put a breakpoint on the first statement in the method.

Occasionally, temporary debug prints are more convenient or necessary. For example:

public static void printResults( String[] name, double[] radius, 
  double[] mass, double calcGravity[])
{
    System.out.println( "name.length = " + name.length );
    System.out.println( "radius.length = " + radius.length );
    ...
Andy Thomas
  • 78,842
  • 10
  • 93
  • 142