Modify You
Modify You

Reputation: 153

Replace Char in Switch Statement

The following is a code to replace vowels of a word with an underscore:

String[] vowelArray = arrayInput;
    for (int x = 0; x < vowelArray.length; x++){
        for (int a = 0; a < vowelArray[x].length(); a++){
            switch (vowelArray[x].charAt(x)){
                case 'A': vowelArray[x].charAt(x) = "_";
                case 'a': vowelArray[x].charAt(x) = "_";
                case 'E': vowelArray[x].charAt(x) = "_";
                case 'e': vowelArray[x].charAt(x) = "_";
                case 'I': vowelArray[x].charAt(x) = "_";
                case 'i': vowelArray[x].charAt(x) = "_";
                case 'O': vowelArray[x].charAt(x) = "_";
                case 'o': vowelArray[x].charAt(x) = "_";
                case 'U': vowelArray[x].charAt(x) = "_";
                case 'u': vowelArray[x].charAt(x) = "_";
                case 'Y': vowelArray[x].charAt(x) = "_";
                case 'y': vowelArray[x].charAt(x) = "_";
            }
        }
    }

However upon compilation, I receive this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The left-hand side of an assignment must be a variable

It occurs in the switch statement on each case. What am I doing wrong?

Upvotes: 3

Views: 2257

Answers (4)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236104

You can't change a String in Java, it's an immutable object. What you can do, is getting the underlying char[] from the String , modifying the array, and creating a new String with it (and don't forget to end each case statement with a break):

String[] vowelArray = arrayInput;
for (int x = 0; x < vowelArray.length; x++) {
    char[] chars = vowelArray[x].toCharArray();
    for (int a = 0; a < chars.length; a++) {
        switch (chars[a]) {
            case 'A': chars[a] = "_"; break;
            case 'a': chars[a] = "_"; break;
            case 'E': chars[a] = "_"; break;
            case 'e': chars[a] = "_"; break;
            case 'I': chars[a] = "_"; break;
            case 'i': chars[a] = "_"; break;
            case 'O': chars[a] = "_"; break;
            case 'o': chars[a] = "_"; break;
            case 'U': chars[a] = "_"; break;
            case 'u': chars[a] = "_"; break;
            case 'Y': chars[a] = "_"; break;
            case 'y': chars[a] = "_"; break;
        }
    }
    vowelArray[x] = new String(chars);
}

Or even simpler, you could use the replaceAll() method to perform the substitution:

String[] vowelArray = arrayInput;
for (int x = 0; x < vowelArray.length; x++) {
    vowelArray[x] = vowelArray[x].replaceAll("[AaEeIiOoUuYy]", "_");
}

Upvotes: 7

ajb
ajb

Reputation: 31699

You can't change a String by changing one character. There's no one method to return a new String with character x (i.e. s.charAt(x)) changed to c, but you can do it like this:

String newString = s.substring(0,x) + c + s.substring(x+1);

That is, characters of s from 0 to x-1 inclusive; then the character c; then characters of s from x+1 to the end. This will work in the boundary cases (x==0 or x==s.length()-1). I'm posting this as a general solution for "what would you do instead of putting charAt(x) on the left side of an assignment", but the other answers are better for this particular problem.

Upvotes: 0

Vivin Paliath
Vivin Paliath

Reputation: 95558

Strings are immutable, and what you're doing (string.charAt(x) = "_";) doesn't really make much sense. You are trying to assign a value to the result of a method invocation. The error message also describes the problem with your statement; the compiler is expecting a variable on the left-hand side.

To do what you want, you can use a regular expression with String#replaceAll.

String newString = string.replaceAll("[AEIOUYaeiouy]", "_");

You can even assign it back to the old string itself:

string = string.replaceAll("[AEIOUYaeiouy]", "_");

Upvotes: 3

nos
nos

Reputation: 229204

Since you cannot change a String in Java, you can build a new string

 for (int x = 0; x < vowelArray.length; x++){
    StrinbBuilder b = new StringBuilder();
    for (int a = 0; a < vowelArray[x].length(); a++){
        switch (vowelArray[x].charAt(a)){
            case 'A'://fall thru
            case 'a'://fall thru
            //and all the other case's
             b.append("_");
           break;
           default:
             b.append(vowelArray[x].charAt(a));
             break;
        }

    }
    vowelArray[x] = b.toString(); //replace the new string
}

Note that your vowelArray[x].charAt(x) should be vowelArray[x].charAt(a)

You can use another approach instead of a big switch as well:

String vowels = "AaEeIiOoUuYy";
if (vowels.indexOf(vowelArray[x].charAt(x)) != -1) {
   //it's a vowel
   b.append("_");
} else {
   b.append(vowelArray[x].charAt(x));
}

Upvotes: 0

Related Questions