简体   繁体   中英

How to find out if a certain character in a String is Uppercase

I checked all other stackoverflow links about my question but the answers there did not work. So I am asking a similar question with the hope of getting a different answer. This is the code from a class:

public String pozisyonHazirla(String param) {

        String oynananHamle = param;   

        for (int n = 0; n < oynananHamle.length(); n++) {
            if (oynananHamle.substring(n, 1).matches("[0-9]") || oynananHamle.substring(n, 1).matches(".")) {

                String donothing="";

            } else if (oynananHamle.substring(n, 1) == oynananHamle.substring(n, 1).toUpperCase()) {

             sonuc = "Figür Hamlesi Yapıldı";
                break;

            } else {

                sonuc = "Piyon hamlesi yapıldı";
            break;
            }

                         }

            return sonuc;  
        }

The question is that this section of the code is not working and in 100% of the cases the iteration returns the else result which is the assigned value to the sonuc variable.

 } else if (oynananHamle.substring(n, 1) == oynananHamle.substring(n, 1).toUpperCase()) {

             sonuc = "Figür Hamlesi Yapıldı";
                break;

My second question without opening another thread is that in the first case there is a section where I want nothing to be done if the "if" statement is viable so I put something like " String donothing ="";" . Is there anyway to improve this piece of code? Thank you in advanace for your answers and comments...

Use isUpperCase() :

char c = 'A';
boolean upper = Character.isUpperCase(c); // true

Concerning that unused doNothing code block, change:

if (oynananHamle.substring(n, 1).matches("[0-9]") || oynananHamle.substring(n, 1).matches(".")) {
    String donothing="";
} else if (oynananHamle.substring(n, 1) == oynananHamle.substring(n, 1).toUpperCase()) {
    sonuc = "Figür Hamlesi Yapıldı";
    break;
} else {
    sonuc = "Piyon hamlesi yapıldı";
    break;
}

to

if (oynananHamle.substring(n, 1).equals(oynananHamle.substring(n, 1).toUpperCase())) {
    sonuc = "Figür Hamlesi Yapıldı";
    break;
}

if (!(oynananHamle.substring(n, 1).matches("[0-9]") || oynananHamle.substring(n, 1).matches(".")) {
    sonuc = "Piyon hamlesi yapıldı";
    break;
}

EDIT:

(full code)

public String pozisyonHazirla(String param) {
    String oynananHamle = param;
    String sonuc = "";

    for (int n = 0; n < oynananHamle.length(); n++) {
        if (oynananHamle.substring(n, 1).equals(oynananHamle.substring(n, 1).toUpperCase())) {
            sonuc = "Figür Hamlesi Yapıldı";
            break;
        }

        if (!(oynananHamle.substring(n, 1).matches("[0-9]") || oynananHamle.substring(n, 1).matches(".")) {
            sonuc = "Piyon hamlesi yapıldı";
            break;
        }
    }

    return sonuc;
}

The problem is == does not compare the contents of strings, it just checks whether it is the same object reference. You need to use .equals() to compare the actual strings.

However you don't even need to do that since you can simply test the individual characters, which will be a lot more efficient than making substrings.

for (int i = 0; i < param.length(); i++) {
    char c = param.charAt(i);
    if ((c >= '0' && c <= '9') || c == '.') {
        continue; // go to next character
    } else if (Character.isUppercase(c)) {
        return "Figür Hamlesi Yapıldı";
    } else {
        return "Piyon hamlesi yapıldı";
    }
}

Hello I thank both of you. The answer is a mixture of both ideas and is as follows:

 for (int i = 0; i < oynananHamle.length(); i++) {
            char c = oynananHamle.charAt(i);
            //if ((c >= '0' && c <= '9') || c == '.') {
               // continue; // go to next character
             if (Character.isUpperCase(c)) {
                sonuc = "Figür Hamlesi Yapıldı :" + c + " "+i;
                 break;
            }
            if (!(c >= '0' && c <= '9' || c == '.')) {
                sonuc = "Piyon hamlesi yapıldı :" + c + " "+i;
                break;
            }
        }

Of course there is a return sonuc statement at the end. Thank you once again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM