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

Runing trough a String Array?  RSS feed

 
Josh Tambini
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the following code..which trows me an exception:


i found out that the exception comes from this section:
"chr=g[i].charAt(i);"
when the index is 0 all is fine...but when it gets higher then 0...it trows an exception...why???
the g[] array length is 3...so what is the prob?
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need the "charAt" part in the code, since all the strings in your array are 1 character long. Just try:
chr = g[i];
and see if it works. Also your loop will not go to the last element because of the condition you have specified. Change it to "i<g.length" to loop through the entire array.

Originally posted by Josh Tambini:
i have the following code..which trows me an exception:


i found out that the exception comes from this section:
"chr=g[i].charAt(i);"
when the index is 0 all is fine...but when it gets higher then 0...it trows an exception...why???
the g[] array length is 3...so what is the prob?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line

is getting the ith character of the ith string. Thusr, in the first string, it is getting the first character. In the second string, it is getting the second character -- wait, the second string doesn't have 2 characters. Better throw an IndexOutOfBounds exception.

What you want to say is:


which gets the first character of the ith string.
[ December 07, 2004: Message edited by: Joel McNary ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!