I'm working on an assignment for one of my classes. I'm very new to java in general and for this problem I was asked to only use loops and the charAt(); command to reverse the midpoint of a string. However, I came to an issue when I try to reverse the string after the midpoint. It gave me an exception and I don't know how to make of it since it looks correct to me. Any help would be appreciated.
import java.util.Scanner;
public class PS4Reverse {
public static void main (String [] args) {
String x = "";
String t = "";
String full = "";
String rev = "";
String complete = "";
Scanner user = new Scanner(System.in);
System.out.println("Enter a string.");
x = user.nextLine();
int real = x.length();
int half = x.length();
half = half / 2;
int i = 0;
for (i = 0; i != half; i++)
{
char n = x.charAt(i);
full = full + n;
}
for (i = i; i != real; i++)
{
char n = x.charAt(i);
t = t + n;
}
int back = t.length();
System.out.println(back);
for (i = back; i != 0; i--)
{
char n = t.charAt(i);
rev = rev + n;
}
complete = full + rev;
System.out.println("Original String:\t\t" + x)
System.out.println("Reverse String:\t\t" + complete);
}
}
Thank all y'all very much in advance!