0

I' m kind of new to Java, and for homework I got this exercise:

write a java program which can take a word from the keyboard and show it reversed ("hello" --> "olleh")

And this is how I wrote the program

import java.util.*;
import java.lang.*;
public class Inverti {
     public static void main(String[] args) {
     System.out.println("digiti una parola o una frase, essa sara' stampata a video alla              rovescia");
     Scanner tastiera = new Scanner(System.in);
     String invertire = tastiera.nextLine();
     for (int i = 0; i < invertire.length();) {
     invertire.replace(invertire.charAt(i), invertire.charAt(invertire.length() - i));
     i++;
         }
System.out.print(invertire);
}
}

(I'm Italian, so consider that "tastiera" means "keyboard" and "digiti una parola o una frase, essa sarà stampata a video alla rovescia" means "type a word, it will be shown reversed")

The program gets compiled, but then, after typing the word:

exception in thread main java.lang.stringindexoutofboundsexception: string 
    index out of bounds: 6 (if string.length() is 6) or 5 (if string.length() = 5).

I looked for answers to other similar questions before posting, but nothing helped me, sorry if the question' s format maybe isn't right.

6
  • 1
    What has Windows-8 got to do with this? Commented Jan 12, 2014 at 14:00
  • 2
    The value of i is 0 at the first iteration. So charAt(invertire.length()-i) will throw the exception. Commented Jan 12, 2014 at 14:01
  • 1
    replace returns a new String, you should assign the result. Commented Jan 12, 2014 at 14:04
  • You should always indicate the specific line that was flagged in the exception traceback. (Not that it's hard to figure out here.) Commented Jan 12, 2014 at 14:05
  • Understand that in Java a String object is immutable, meaning that any manipulation you do on a String (such as replace) does not modify THAT string but instead returns a new String which you must assign somewhere. Commented Jan 12, 2014 at 14:07

2 Answers 2

1

In case your line is 6 length long that means the range of possibilities are [0, 5].
Then if we take i as 0 and invertire as 6 will get the following evaluation:

invertire.charAt(6 - 0)

The result is of course invertire.charAt(6) that doesn't appears in the range, and it's what causing the exception.

Fix:

invertire.charAt(invertire.length() - i - 1)
Sign up to request clarification or add additional context in comments.

Comments

0

I'd do something like this:

Scanner tastiera = new Scanner(System.in);
String input = tastiera.nextLine();
StringBuilder inverse = new StringBuilder();
for (int i = 0; i < input.length();i++) {
inverse.append(input.charAt(input.length()-1-i));
}
System.out.print(inverse);

2 Comments

If you're using a StringBuilder, why don't you simply do String inverse = new StringBuilder(input).reverse().toString(); ?
yes thats the easiest way to achieve String reversal. I was not trying to take a short cut here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.