AHA27
AHA27

Reputation: 31

Java - I can't replace char 'Á' with another char in String

Why this "Á" alphabet doesn't change? this code works on the other alphabet but Á.

public class Convert {
    static String turkishCharacterConverter(String s) {
        StringBuilder x = new StringBuilder();
        char[] charArr = s.toLowerCase().toCharArray();
        for (int i = 0; i < charArr.length; i++) {
            if (charArr[i] == '˝') {
                x.append('I');
            } else if (charArr[i] == '˛') {
                x.append("S");
            } else if (charArr[i] == '') {
                x.append("G");
            } else if (charArr[i] == 'ˆ') {
                x.append("O");
            } else if (charArr[i] == '¸') {
                x.append("U");
            } else if (charArr[i] == 'Á') {
                x.append("C");
            } else {
                x.append(s.charAt(i));
            }
        }
        return x.toString();
    }
    public static void main(String[] args) {
        System.out.println(turkishCharacterConverter("˝˛˝˝nda"));
        System.out.println(turkishCharacterConverter("ˆlÁt¸¸m"));
    }
}

and java print

ISIGInda

OlÁtUGUm ( i want to make java print "OlCtUGUm" )

Upvotes: 0

Views: 393

Answers (4)

MariuszS
MariuszS

Reputation: 31557

Maybe because unicode contains duplicated characters.

Upvotes: 0

Antonio Carlos
Antonio Carlos

Reputation: 780

Try to use:

'\u00c1'

instead of 'Á'

Upvotes: 0

Vinayak Pingale
Vinayak Pingale

Reputation: 1315

It is making Á as á because of using lowercase. try using this

if(charArr[i] == 'á')

Upvotes: 2

assylias
assylias

Reputation: 328568

Because of char[] charArr = s.toLowerCase().toCharArray();

You are comparing Á with its lower case á. The other characters are already in lower case so it works fine for them.

Upvotes: 8

Related Questions