I'm trying to get a user input and print the word starting from the last character and adding the previous one to the next line with one less space in the front, looking like it's aligned to the right.
but it shows there's an error in: System.out.print(word.charAt(count));
Scanner input = new Scanner(System.in);
System.out.print("Enter word to print: ");
String word = input.nextLine();
System.out.println();
int line, space, count = -1;
for (line = word.length(); line > 0; line--){
for (space = line; space > 0; space--){
System.out.print(" ");
count++;
}
for ( ; count <= word.length(); count++){
System.out.print(word.charAt(count));
}
System.out.println();
}
error shows as:
Exception in thread "main java.lang.String.IndexOutOfBoundsException: S
at java.lang.String.charAt(String.java:658)
at HelloWorld.main(HelloWorld.java:22)