0

I'm making an array that outputs a user's input in reverse order. My program works however I keep getting this message, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Reverse.main(Reverse.java:26)"

I'm looking at line 26 which is, "System.out.println(number[x]);" but I don't see what's the matter with this. Why is this occurring?

    for(int x= number.length -1; x<SIZE; x--) 
    System.out.println(number[x]); 
   }
 }
4
  • 1
    X appears to be decremented infinitely, should it be x >= 0;x-- Commented Mar 4, 2015 at 1:05
  • Come on. Fire up your debugger. Add some printlns. The exception tells you exactly what is wrong and where. Put some effort in before posting a question! Commented Mar 4, 2015 at 1:05
  • @John3136 I started learning Java yesterday; so I'm new at coding. I wouldn't be posting if I didn't already attempt to debug and still did not understand. Have a great day salterino. Commented Mar 4, 2015 at 1:12
  • x >= 0 should fix it Commented Mar 4, 2015 at 1:23

3 Answers 3

3

The loop is repeating whilst x is less than SIZE. SIZE is 10 so it keeps going beyond zero and checks for the location -1.

Change the check in the second for loop to be

x >= 0
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! That makes sense, I didn't think about the x<SIZE going beyond zero, I thought it would automatically stop at zero.
Remember the for loop will repeat whilst the middle expression is satisfied, so if you're counting down you need to check for the minimum value rather than the maximum value. You're welcome!
3

You are attempting to output the elements in reverse order, but your for loop condition is similar to the y for loop that enters integers into the array.

But counting x backwards, the condition x<SIZE will always be true, even if you run off the beginning of the array. This explains the ArrayIndexOutOfBoundsException.

You must stop the x for loop once x has passed 0.

for(int x = number.length - 1; x >= 0; x--) 

Comments

1

The problem is in your for loop -

for(int x= number.length -1; x<SIZE; x--) 
    System.out.println(number[x]);  

After setting x to number.length-1 x indefinitely decrements. Once x become negative and the number[] array is trying to get value using negative index for example number[-1]. Then ArrayIndexOutOfBound exception occurred. Use the following code snippet instead -

for(int x= number.length -1; x>=0; x--) 
        System.out.println(number[x]);  

Your code perfectly showing output because the exception occurred after the output has been displayed. The code displays all element from number[] array and then the exception occurred.

Thanks a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.