This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with an iteration  RSS feed

 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m having troubles with my project but this time is with a for sentence.
What i think that i�m doing is try a letter typed for the user and compare it with all the letters from a given word.
So if a typed "z" and my word is "Metallica" in the first iteration will be false and it will try the else but 0!=palusuario so "i" it is increased at 1...until "i" it is 9 (9==9)accord to the length of "Metallica" it should show System.out.println(tama�oajugar);
String parte=m.imprimeMono(i);
System.out.println(parte);
System.out.println("Tienes "+intentos+ " intentos m�s");
but i got this La palabra a adivinar tiene 18 palabras
Teclea una letra: a
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
at Rock.obtieneLetra(Rock.java:68)
at Menu.main(Menu.java:39)


Am i right or how it should be work???


[ April 06, 2007: Message edited by: Ilhuicahua Xicohtencatl ]
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i edit my posts???
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilhuicahua Xicohtencatl:
can i edit my posts???

Yes, you can. Just click on the paper/pencil icon.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilhuicahua Xicohtencatl:
...Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
at Rock.obtieneLetra(Rock.java:68)
at Menu.main(Menu.java:39)...

Your code is trying to access index 18 of an array that has a length of 18 or less (so the array index is out of bounds). This is happening at line 68 of your Rock.java file.

I don't think the code you posted is the problem, because I don't see an array.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about my bad spanish but I've included a rough translation of my english:
I don't know if this is important, but in your code the line (No s� si es importante, pero esta l�nea )will never be true because i will always be less than tama�oajugar. This is due to this line (nunca ser� verdad porque i siempre ser� menor de tama�oajugar. Esto es debido a esta l�nea):
[ April 06, 2007: Message edited by: pete stein ]
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how can i compare when i==tama�oajugar???
Is my logic ok in the iteration?


[ April 06, 2007: Message edited by: Ilhuicahua Xicohtencatl ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the problem causing the exception...

Here is an example showing what happens...

[ April 06, 2007: Message edited by: marc weber ]
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is an example showing what happens...




Thanks for the reply but i still don�t get it.
Out of the for you can "see" the last value until x reach, that will be 10.
Why it is x out of bounds on System.out.println(charArray[x]) if i don�t want to work or do something with that i just wanna see it.

For example here in this :let us say that i�m working with "Metallica" again
so length=9, so it starts to run until 9 and it will ask to me 9<=9, yes, so compares again and jump to if(9==tama�oajugar)(tama�oajugar is 9 ), don�t will show 9@9??

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to show it, in
there are 5 int items in the myInt array: myInt[0] to myInt[4]. Arrays go from 0 to (length - 1), not from 1 to length. Thus myInt[5] doesn't exist. It is out of bounds.
[ April 06, 2007: Message edited by: pete stein ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:


c.length is 3...
c[0] is 'a'
c[1] is 'b'
c[2] is 'c'
c[3] is OUT OF BOUNDS.


x is 0, and c.length is 3.
0 is < 3, so the loop body executes, then increments x to 1.
1 is < 3, so the loop body executes, then increments x to 2.
2 is < 3, so the loop body executes, then increments x to 3.
3 is NOT < 3, so the loop exits.


c[x] is c[3], but this is OUT OF BOUNDS.
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks now i get it!!!
Hey i have another question but it�s about the form about posting.
If i�m done with this thread but there�s another problem shall i create a new topic or must to be posted in the same thread???
In case that doesn�t be allowed : �How can i work with a char without the importance if it a lowercase or a uppercase?

I read the api and found in the Character class the methods isUpperCase and isLowerCase but doesn�t work

[ April 07, 2007: Message edited by: Ilhuicahua Xicohtencatl ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably best to start a new thread.

What are you checking for with this statement?:

This will always be true since a character is always either upper or lower case (I think). Are you trying to test whether the palusuario character is the same as another character but are concerned about it being upper or lower case?

Two ways I've done this include:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!