wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes StringIndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringIndexOutOfBoundsException" Watch "StringIndexOutOfBoundsException" New topic
Author

StringIndexOutOfBoundsException

Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

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.


When you do things right, people won't be sure you've done anything at all.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Jul 07, 2010
Posts: 26
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Janeice wrote:If all you're doing is getting the last element, then -1 is the way to go.

All clear
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Rob wrote:Real programmers start at 0

+0. I mean +1
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
Who was it who had 0.5 as a suggestion for array indices in his signature?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 29, 2008
Posts: 12617

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.
 
 
subject: StringIndexOutOfBoundsException
 
Similar Threads
Remove Chars from a String?
Creating a Reglar Expressions
ExamLab: invalid expression String [] s = files.split("[(\\s)+]");
question on overloaded methods
Java String pool problem