0

I'm taking care of some other methods and I don't know what to do with this one. I want to change the order of the string inside the array (not the order of the string*s*), but this isn't accepted. Any ideas?

public void invert() {

        for(int i = 0; i < array.length; i++){
            for(int j = 0, k = array[i].length() - 1; j < k; j++, k--){
                char a = array[i].charAt(j);
                array[i].charAt(j) = array[k].charAt(k); //ERROR HERE
                array[i].charAt(k) = a;                  //AND HERE
            }
        }
    }

EDIT: I'll leave here what I mean. I have an array = {"Hello", "Goodbye"} I want to change it to {"olleH", "eybdooG"}

1
  • 1
    Use new StringBuilder.reverse(). Commented Dec 11, 2013 at 17:57

6 Answers 6

4

Java string are immutable. You can't change them.

(But you can convert the string to a StringBuilder - http://docs.oracle.com/javase/tutorial/java/data/buffers.html - which is essentialy a mutable string, change the characters, and then convert the StrignBuilder back to String.)

Try this code (I haven't tested it, but I hope it works):

    for(int i = 0; i < array.length; i++) {
        StringBuilder b = new StringBuilder(array[i]);
        for(int j = 0, k = b.length() - 1; j < k; j++, k--){
            char a = b.charAt(j);
            b.setCharAt(j, array[k].charAt(k));
            b.setCharAt(k, a);                  
        }
        array[i] = b.toString();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked! Just one little thing, it was array[i].charAt(k). Was getting "java.lang.ArrayIndexOutOfBoundsException: 12" and couldn't figure out where hehe. Thanks!!
Well, then you can use just this: b.setCharAt(j, b.charAt(k));.
2
array[i].charAt(j) = array[k].charAt(k); //ERROR HERE
  • array[i].charAt(a) returns a value not a variable. You are trying to assign a value to a value which doesn't make any sense.
  • java String is immutable. You can't change it.
  • Use StringBuilder which has setCharAt(int index, char ch); function which is what you are probably wanting.

Comments

0

The most simple way is, to reverse letter with StringBuilder.reverse() method. Try,

 for(String str : array){
   System.out.println(new StringBuilder(str).reverse());
 }

Comments

0

Just use this on every String in your array:

String reversed = new StringBuilder(stringFromArray).reverse().toString();

Comments

0

try doing new StringBuilder(array[i]).reverse().toString();

Comments

-1

you would have to create a substring.

array[i]= array[i].substring(0,j) + array[k].charAt(k) + array[i].substring(j+1);

This would do the required edit i beleive

Comments

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.