Reputation: 39
I'm trying to reverse a string in Java but I get an error I don't understand when I run it:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:658)
at ReversingText.reverse(ReversingText.java:13)
at ReversingText.main(ReversingText.java:27)"
This is my code:
import java.util.Scanner;
public class ReversingText {
public static String reverse(String text) {
int i = (text.length() - 1);
String letter = "";
while (i >= 0) {
char character = letter.charAt(i);
letter += character;
i--;
}
return letter;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type in your text: ");
String text = reader.nextLine();
System.out.println("In reverse order: " + reverse(text));
}
}
Not sure where the problem is, as I think index starts at 0 so I decrease length of the string by 1 to find index?
Thanks for any help
Upvotes: 0
Views: 436
Reputation: 2340
The problem at this line:
char character = letter.charAt(i);
You should manipulate with text
variable instead of letter
:
char character = text.charAt(i);
Also you can achieve that in another approach using Stack
data structure:
public static String reverse(String text) {
Stack<Character> st = new Stack<Character>();
String rtext = "";
for(int i = 0; i < text.length(); i++){
st.push(text.charAt(i));
}
while(!st.empty()){
rtext = rtext + st.pop();
}
return rtext;
}
Upvotes: 0
Reputation: 31
The problem were in this line: char character = letter.charAt(i); You've worked with letter instead of text)
Here is the working code:
import java.util.Scanner;
public class ReversingText {
public static String reverse(String text) {
int i = text.length()-1;
System.out.println("length: " + i);
String letter = "";
while (i >= 0) {
char character = text.charAt(i);
letter += character;
i--;
}
return letter;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type in your text: ");
String text = reader.nextLine();
System.out.println("In reverse order: " + reverse(text));
}
}
Upvotes: 0
Reputation: 55
The problem is you are finding the char at index i
in the empty String letter
. When the while
loop runs for the first time, i = 3
and there is no char at index 3 in letter
.
Instead, call charAt(i)
on text
:
public static String reverse(String text) {
int i = text.length() - 1;
String letter = "";
while (i >= 0) {
char character = text.charAt(i);
letter += character;
i--;
}
return letter;
}
Alternatively, for a much simpler way of reversing text, use:
public static String reverse(String text) {
return new StringBuilder(text).reverse().toString();
}
See also: Reverse a string in Java
Upvotes: 1
Reputation: 119
Another tip:
In the end, remember to close the reader.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type in your text: ");
String text = reader.nextLine();
System.out.println("In reverse order: " + reverse(text));
reader.close(); // add this line
}
Upvotes: 0
Reputation: 1144
There were other mistakes. This is the correct programme.
public class ReversingText {
public static String reverse(String text) {
int i = (text.length() - 1);
String letter = text;
String reverseletter = "";
while (i >= 0) {
char character = letter.charAt(i);
reverseletter += character;
i--;
}
return reverseletter;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type in your text: ");
String text = reader.nextLine();
System.out.println("In reverse order: " + reverse(text));
}
}
Upvotes: 1
Reputation: 937
The problem is you are trying to get the character of variable letter
(which starts as an empty string, therefore with no length and thus no valid index) instead of text
:
char character = letter.charAt(i);
Try this:
char character = text.charAt(i);
Upvotes: 2