Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+15 votes

I just finished doing our PracticeIt assignment and I was stumped on the printReverse question.

After initializing phraseLength to word.length(), I put in a for loop initializing my loop control variable i to phraseLength and counted down to 0.

I continued to get an error:
threw StringIndexOutOfBoundsException:
String index out of range: 12
CodeStepByStepJavaTestHarness.printReverse(CodeStepByStepJavaTestHarness.java:4)

I don't quite understand why this doesn't work. I figured out what code works, but I just cannot figure out why this code is ending in error.

Any help is appreciated!

asked in CSC211_Winter2018 by (1 point)

1 Answer

+11 votes
 
Best answer

When you assign phraseLength = phrase.length(), it will store the number more then the index number in the string. Because the string index always start with 0 base, but when you count the length of the string, it will start with number 1. So, it will give you this error: StringIndexOutOfBoundsException.

answered by (1 point)
selected by
...