• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String index out of range

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3287
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message will also tell you the line of code that is causing the problem.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic