| Author |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
|
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
|
|
Hello,
My program:
Please advise
Thanks in anticipation
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
|
How many elements are actually in your words array? I would expect there to be no more than 2. So, what do you think should happen when your for-loop tries to access the third element?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
|
|
fred rosenberger wrote:How many elements are actually in your words array? I would expect there to be no more than 2. So, what do you think should happen when your for-loop tries to access the third element?
I am trying that If there is no more elements in the array then the program should go to the other block i.e. if(words[i].equals("")){ and loop should print "array is null" 3 times.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
No. That isn't how arrays work. If you try to get the third entry in an array which only has two entries, then that exception is going to be thrown. You aren't going to get null. And you especially aren't going to get an empty string, which is not null.
|
 |
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
|
|
Okay, I want to run loop 5 times by print words.length; e.g. 2 then exit from array and print something to rest of the three elements of the loop.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Then do that. You will need to know that an array has a "length" attribute which tells you how many elements the array has. In your code, to tell how many elements are in your "words" array you use this:
Oh wait a minute. It looks like you already know that. So use it. You'll need an if-statement to tell whether the current value of the loop index corresponds to an element in your array or not.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
Farakh khan wrote:Okay, I want to run loop 5 times by print words.length; e.g. 2 then exit from array and print something to rest of the three elements of the loop.
you can't "exit from array". That doesn't make any sense.
One possible thing you can to is test to see how many elements are in the array. Have one loop that runs that many times. Then, have another loop that runs (5 - that many times).
Not the best solution, but not a bad one for a beginner.
|
 |
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
|
|
Hello,
OUTPUT:
The output is wrong there must be 3 elements "array is null" and 2 must be "array is not empty". Please advise
thanks again
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Farakh khan wrote:Hello,
OUTPUT:
The output is wrong there must be 3 elements "array is null" and 2 must be "array is not empty". Please advise
thanks again
Based on the output, do you really need advise? Can't you take a shot at what could be wrong?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
|
|
output:
Now the output is okay. Is that right or I can refine more?
Thanks again
|
 |
 |
|
|
subject: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
|
|
|