0

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.

Marc
  • 15
  • 1
  • 4

4 Answers4

1

Your problem lies here:

String reversedString = " ";

After a few lines you write:

j = reversedString.charAt(max); // while the reversedString.length() == 1

That's why you get your error. You've confused x.length() which you've inputed to max (6 apperently) and reversedString which always equals " " and never has a length higher than 1.

MaxG
  • 1,079
  • 2
  • 13
  • 26
1

It looks like that you are writing a program to reverse a string. But at the first glance, I think you are far from the solution.

But don't worry! The reason why the exception is thrown is that you are getting the character at the max index in the string " ". The string " " only has one character - a space. This means that it only has one index, 0. If you try to access the character at index 6 or 7 or 8 or some value other than 0, you get an exception!

Why not use the reverse method provided in the StringBuilder class? It is insanely easy to use! Put this code in your reverse method:

StringBuilder builder = new StringBuilder(x);
builder.reverse();
x = builder.toString();
System.out.println(x);

Let's look at this line by line.

StringBuilder builder = new StringBuilder(x);

This creates a new StringBuilder with an initial string of x, the argument passed into the method.

builder.reverse();

This line reverses the string in the StringBuilder, aka x.

x = builder.toString();

This assigns the reversed string to x.

System.out.println(x);

You know, just prints the reversed string.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The linej = reversedString.charAt(max); is giving you error.Since reversedString conatins empty string of length zero.But you are accessing max index from that.

Note:The logic you have written for reversing string is not correct.Below code may help on that

for(int i = x.length() - 1; i >=0 ; i--)
{
  char c = x.charAt(i);
  reversedString = reversedString+c;
}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
-1

reversedString has an initial length of 1, so its' initial maximum valid index will be 0. For any string x longer than 0 (any non-empty string) max = x.length() will cause a StringIndexOutOfBoundsException in the first iteration.

yaccob
  • 1,230
  • 13
  • 16