• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing more then one object in an array?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to create a program that plays an elimination game. The user has to input the number of players and the number of "cycles" or how much the program counts by. So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner. As of now my current plan to change any variables that the cycle lands on to 999 and keep going until there is one winner. But right now my code only changes one value to 999 and then stops. How can I get my code to change any value it lands on to 999 and keep going until a winner has been picked? Here is my code

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I was able to figure out why my code only changes one number. It's because I told it to change the cycle number and not the number it lands on. I tried adding something to my code to do this. The code compiles but when I try to run it I get an error. Here is my new code



and here is the error I am getting

"java.lang.ArrayIndexOutOfBoundsException: 998
at numberPicker.main(numberPicker.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"

any ideas on how to fix this?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:and here is the error I am getting

"java.lang.ArrayIndexOutOfBoundsException: 998
at numberPicker.main(numberPicker.java:42)


That tells you several things:

1. You tried to access an array outside of its actual bounds.

2. You tried to access element 998.

3. You did that at line 42, which is in a method named "main".

So you should look at that line and find out why you are trying to access element 998 of an array which isn't that big.
 
John Sing
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
John Sing wrote:and here is the error I am getting

"java.lang.ArrayIndexOutOfBoundsException: 998
at numberPicker.main(numberPicker.java:42)


That tells you several things:

1. You tried to access an array outside of its actual bounds.

2. You tried to access element 998.

3. You did that at line 42, which is in a method named "main".

So you should look at that line and find out why you are trying to access element 998 of an array which isn't that big.


Thank you for being clear and to the point. I was able to fix the error quite easily after you cleared it up for me.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! It seems like the books and tutorials and whatnot which people learn Java from don't actually teach people how to read stack traces. That's a skill which has to be learned too.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Excellent! It seems like the books and tutorials and whatnot which people learn Java from don't actually teach people how to read stack traces. That's a skill which has to be learned too.
I have seen stack traces mentioned in books but don't remember seeing a description of what it means.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!