public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String word, end;
int length = 1; //length of int is determined here so it can be incremented in loop
System.out.println("Enter a word: ");
word = scan.next();
while (1 == 1) {
end = word.substring (word.length()-length);
System.out.print (end + " ");
length = length + 1; //variable length is incremented so after loop 2 chars from the end will be printed, then 3 and so on until none are left.
if (word == null) {
System.out.println("");
}
}
I am trying to achieve the following i.e. if "JAVA" was typed in what would return is "A VA AVA JAVA". although that is returned I keep getting the following error and have tried about everything I can to fix it but none of them work. Is there any easier method I can use or if a little modification on the above code will fix the problem, please inform me!
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1"