My assignment is to reverse a string and print the reversed string.
Here is my code
import java.util.*;
import java.io.*;
public class reverse1
{
public static void reverse(String x) throws IllegalCharacterException
{
char j;
int max;
String reversedString = " ";
for(int i = 0; i < x.length() - 1; i++)
{
max = x.length();
j = x.charAt(i);
j = reversedString.charAt(max);
i--;
}//end for loop
}//end method
public static void main(String[] args) throws IllegalCharacterException
{
Scanner keyboard = new Scanner(System.in);
String s;
System.out.println("Please enter a string");
s = keyboard.next();
reverse(s);
}//end main
}//end class
I keep getting this error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(Unknown Source)
at reverse1.reverse(reverse1.java:14)
at reverse1.main(reverse1.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I had checked the line numbers mentioned but still don't understand the problem. I did change the x.length() to x.length() - 1 because a friend had told me to do that, but I keep getting the same error.