aspose file tools*
The moose likes Beginning Java and the fly likes String index out of range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String index out of range" Watch "String index out of range" New topic
Author

String index out of range

Sarah Jimberman
Greenhorn

Joined: Feb 25, 2011
Posts: 1
So, I'm making a Turing Machine that takes a string of a's and b's in the first argument of the command line and then replaces all the a's with b's. Everything I've worked out is working fine except for an error I keep getting at the end of my output that reads:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: "
and then after the colon is the number of characters I entered as my first argument in the command line.
Not sure what code is relevant to this problem, but the only thing I can think of that would affect this is "String tape = args[0];" which should be the line of characters I'm running through my machine.
Thank you for any help you can provide, as this has been perplexing me for awhile.

~Sarah
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
The error message will also tell you the line of code that is causing the problem.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
The fact that it's "at the end of [your] output" leads me to suspect that you are using some form of String.substring(); that method takes a beginning and ending index, but it treats the string as 0-based, i.e., "1234".substring(0,0) will give you "1", and "1234".substring(4,4) will give you a StringIndexOutOfBoundsException. If you are going through the string, you need to go from 0 to (String.length() - 1), or perhaps while i < string.length().

It would be helpful if you gave us more to go on. I'm just guessing because you've given us no code and an incomplete error message report.

rc
 
wood burning stoves
 
subject: String index out of range