• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays - Exception error  RSS feed

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I must say these forums are incredibly helpful. I've got another question regarding arrays. I'm writing a connect four program and, to set up the game, I have a few dialog boxes to set the game up.

Here's the problem. The first dialog box asks how many players. Technically, the user can enter any number they want. If it is greater than 4, it throws the following error:
sException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Input.main(Input.java:26)

What I need to do is set some exception rule to prevent the user from entering anything lower than 2 and anything greater than 4.

Remember, I want to learn how to fish, not have you fish for me.

Here's the code:

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First.... what do you think "Array Index Out Of Bounds" means.... in words?

What index does your (and all) array start with?

so.... that means the number of elements is (the same number, more, or less) than the last index?

Oh, wait... I thought you were having a similar, but different problem. I'll leave that bit of info for you to help you figure it out though....

Hint: Arrays have a set number of elements when instantiated....
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also just thinking there's two (or more) ways to fry this fish....
you can keep asking for valid input, or just let the user know something like: "well, you chcose 5, but the max is 4. Set for 4 players"

I suppose you can also end the program I suppose, but that's no fun.
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:First.... what do you think "Array Index Out Of Bounds" means.... in words?

Arrays are finite objects. If the error states "Out Of Bounds" it probably means that array doesn't stretch that far, or it is going into a nonexistent object. Which is impossible.

What index does your (and all) array start with?
0. So essentially, the playerArray = new String[3] gives it four "slots."

so.... that means the number of elements is (the same number, more, or less) than the last index?

Oh, wait... I thought you were having a similar, but different problem. I'll leave that bit of info for you to help you figure it out though....

Hint: Arrays have a set number of elements when instantiated....


Not really sure about what you are asking for this last bit.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no. an array instantiated as:


Has 3 slots. The top index is someArray[2].

As for your issue... when a person chooses too many players, the loop goes too long. You have to decide, at the point of input I suppose would be best, how it is you want to handle it. You can instantiate the array after (and allow however many players the user wants by initializing the array with their input), loop around until you get the input you're looking for (with some sort of while loop), or just end the program and yell at the user.

There are other options... these seem to be easiest in my head.
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I played around with it and here's what I got:

It works because of the do loop I put in. However, I don't feel like I understand this "array instantiated" thing...

I changed it to 4, so it will open up 4 "slots".... or 3?
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome.

Check this:


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Coddaire wrote: . . . If the error states . . . or it is going into a nonexistent object. Which is impossible. . . .
It's quite possible. If you write C++ and try access to the 11th member of a 10-member array, you will get something back. You can't tell what it is, and it will probably be nothing but a nuisance to you, but you can still get something. When they designed Java™ they made the JVM throw that exception if you try for something beyond the size of the array. Try myArray[-1], for example. Or create a zero-length arrayThe exception prevents you from gaining access to adjacent memory, because who knows what values you would get back.you should get the same Exception from both examples
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote: . . .
You can only write = {123, 456, 789}; on the same line as the declaration. If on separate lines, you would have to write = new int[]{123, 456, 789};
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Janeice DelVecchio wrote: . . .
You can only write = {123, 456, 789}; on the same line as the declaration. If on separate lines, you would have to write = new int[]{123, 456, 789};


I'm writing that down.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two Java™ Language Specification links:1 2.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!