hello, i wrote a program that changes all occurrences of a letter in a sentence, into a different letter. for instance, "The pink pig flew." if the user wanted to change the 'p' into 't', the output would be "The tink tig flew." But everytime i run it, it tells me StringIndexOutOfBoundsException: String index out of range 7. I don't know what i did wrong, please help!
here is my code:
public class change { public static void main(String[] args) { String first,rest,phrase,chara,newchar; int count; System.out.println("What is the phrase?"); phrase=IO.readString(); System.out.println("What is the letter you want to replace?"); chara=IO.readString(); System.out.println("What is the letter you want in place?"); newchar=IO.readString();
count=-1; int length=(phrase.length()); length=(length-1); while (count!=length) { count=(count+1); first=(phrase.substring((count),(count+1))); rest=(phrase.substring(count+1)); if (first.equals(chara)) { phrase=(first+newchar+rest); } else { phrase=(first+cha ra+rest); } } IO.printString(phrase); } }