I'm trying to try a method that accepts a String object and displays its contents backwards.
Here is the method I wrote:
public static String backwards(String word)
{
final int SIZE = word.length();
char[] wordArray = word.toCharArray(); //converts String parameter to char array
char[] backwardArray = new char[SIZE];
for(int i = 0; i < SIZE; i++)
{
backwardArray[i] = wordArray[SIZE - i];
}
String backwardString = new String(backwardArray);
return backwardString;
}
I get the following exception thrown when I run the program:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at BackwardString.backwards(BackwardString.java:32)
at BackwardString.main(BackwardString.java:16)
Press any key to continue . . .
There's probably an easy solution to this, but I don't see it. Could anybody help point me in the right direction?
Thanks.