This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringIndexOutOfBoundsException

 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok, I know the problem, but I cant find how to fix it after googling and reading thru my book ( BIG JAVA, 3rd Edition by Cay Horstmann, Wiley 2008). I was thinking just taking the .length out and replacing it with a 3 that way it reads the last char, but I'm not sure if theirs a proper way to do it with the same code or some modification.

The project says to correct the errors, so how would I correct it?





Thanks as always.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hate that Big Java book...

This is because the indexing starts at 0. The length of the string will always give you one more than the last character....

Check this out:


You can also replace the 3 with (s.length() - 1), although it's not pretty.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it. charAt gets an index starting at 0. length returns the length of the string. Imagine that the string was only one character, its length would be 1. And the index of the last character would be ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:You can also replace the 3 with (s.length() - 1), although it's not pretty.

You mean hard-coding the index of the last character is not pretty ?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just worry about getting into the habit of using +1 or -1 to correct a logic error. Seems messy.

If the program were even a little more complex than just outputting the last character, I might look for a different way.

For example...

 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:Hate that Big Java book...

This is because the indexing starts at 0. The length of the string will always give you one more than the last character....

Check this out:


You can also replace the 3 with (s.length() - 1), although it's not pretty.


I understand that it starts at 0 and the length is 4, so it could never come out correct, but you solution to add a -1 is awesome. I never would have thought of that, thank you.

I grasp the idea, I just dont know what way to go about solving the problems most the time. Eh...the books seems ok to me, I just read what they tell me to.

Im glad I found these fourms you guys are very helpful, to bad I didnt come here last semester when I failed this class =p
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow you. I must be missing something obvious I don't see length()-1 as a nasty way to get the last element of an "entity" whose index starts at 0. How would you return the last element of an array ?

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trick is, generally, to think about things in real words first, then to try to put it into code.

"the length of the String is 4"
"the last character is at index 3"

"3 is one less than 4"
"how can I tell the computer to select the index at one less than the length?"

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:I don't follow you. I must be missing something obvious I don't see length()-1 as a nasty way to get the last element of an "entity" whose index starts at 0. How would you return the last element of an array ?



I agree. If all you're doing is getting the last element, then -1 is the way to go.

But using +1 and -1 in more complex situations, when things could be fixed LOGICALLY is bad form, no?
I only say this because I found (after reading and listening to my elders ) that I learned bad habits from people not telling me that there are better ways to handle "one off" situations.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice wrote:If all you're doing is getting the last element, then -1 is the way to go.

All clear
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:You can also replace the 3 with (s.length() - 1), although it's not pretty.

That's one thing from Python I really miss in Java - using negative indexes to start at the end. In Python you can just use s[-1] to get the last character (yes, Python strings have direct indexing as if they are arrays).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The - 1 is unavoidable if you use 0 indexed arrays. It's "swings and roundabouts" when you index arrays. If you start with 0 you are directly giving the computer the offset from the start of the array, but it is less intuitive. That is why for loops use i = 0 and <.
If you use 1-indexed arrays, it is easier to understand, but the compiler has to add -1 to all indices.

1-based arrays might be better because they are easier for the programmer to understand.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you use 1-indexed arrays, it is easier to understand, but the compiler has to add -1 to all indices.

Of course you run into other problems. I had a piece of VB (ugh!) code where I had to strip off a leading and trailing piece of text. My initial code:
And while this works perfectly in Java (being 0-based), VB is 1-based so I ended up cutting off with the right-most character of LeftPart still in my string, and the last character of the string missing. I ended up adding a +1 to the first Len(LeftPart).
For those that don't understand what I mean: if Value = "My name is Rob Prime", LeftPart = "My name is " and RightPart = " Prime", I ended up with " Ro" instead of "Rob".

1-based arrays might be better because they are easier for the programmer to understand.

Real programmers start at 0
I've talked to my manager about this; normal people start at 1, true, but programmers of languages like C, C++ and Java start at 0 fast enough. For me it feels unnatural to start at 1 in programming. (Although JDBC is of course the exception to the rule of Java starting at 0; it starts at 1...)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob wrote:Real programmers start at 0

+0. I mean +1
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who was it who had 0.5 as a suggestion for array indices in his signature?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Janeice DelVecchio wrote:You can also replace the 3 with (s.length() - 1), although it's not pretty.

That's one thing from Python I really miss in Java - using negative indexes to start at the end. In Python you can just use s[-1] to get the last character (yes, Python strings have direct indexing as if they are arrays).

Groovy also uses this notation. This simplifies things a lot.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, a lot of languages handle ranges this way... On the one hand I like it because it's short, on the other I don't because it's using linear math in a pseudo-circular way. I have no better answer without giving some sort of symbol new syntax, so meh.

0.5 seems a reasonable compromise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic