Reputation: 21
So I have a project for school that I am working on and was hoping you guys could look over my code and tell me what I may be doing wrong. I need all numbers 0-9 to be replaced with their word counterparts, but any number above that to stay the same.
this is the code now and my reasoning
Im at my wits end with the 12 not printing onetwo. Ive tried everything right now I havE
for (int i=0; i < stringBuilder.length(); i++)
{
if ((stringBuilder.charAt(i) == '0' || stringBuilder.charAt(i) == '1' || stringBuilder.charAt(i) == '2' || stringBuilder.charAt(i) == '3' || stringBuilder.charAt(i) == '4' || stringBuilder.charAt(i) == '5' || stringBuilder.charAt(i) == '6' ||
stringBuilder.charAt(i) == '7' || stringBuilder.charAt(i) == '8' || stringBuilder.charAt(i) == '9')&& (stringBuilder.charAt(i-1) != '0' || stringBuilder.charAt(i-1) != '1' || stringBuilder.charAt(i-1) != '2' || stringBuilder.charAt(i-1) != '3' || stringBuilder.charAt(i-1) != '4'
|| stringBuilder.charAt(i-1) != '5' || stringBuilder.charAt(i-1) != '6' || stringBuilder.charAt(i-1) != '7' || stringBuilder.charAt(i-1) != '8' || stringBuilder.charAt(i-1) != '9') && (stringBuilder.charAt(i+1) == '0' || stringBuilder.charAt(i+1) == '1'
|| stringBuilder.charAt(i+1) == '2' || stringBuilder.charAt(i+1) == '3' || stringBuilder.charAt(i+1) == '4' || stringBuilder.charAt(i+1) == '5' || stringBuilder.charAt(i+1) == '6' || stringBuilder.charAt(i+1) == '7' || stringBuilder.charAt(i+1) == '8' || stringBuilder.charAt(i+1) == '9'))
{
basically it first runs throught the for loop to find if any of the positions of i have these values then it does the position (i) - 1 to see if it the position before i is also one of the numbers, then it does the same for i + 1 however I get an error about a -1 value range being invalid or something. Can anyone offer help?
Upvotes: 0
Views: 211
Reputation: 193
Since you start with
i = 0
, you cannot use
charAt(i-1)
because -1 is an invalid index.
Upvotes: 1