• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of range

 
Sarah Jimberman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message will also tell you the line of code that is causing the problem.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic