Simple string.equals () if statement does not work Java

I'm going to go crazy. Maybe this is the reason I work 12 hours .... but why my if statment will not evaluate true at startup if (band.equals("4384")? I type bandon the screen and it reads 4384, but it will not evaluate true. I use .equals () so many times with a problem, what am I doing wrong?

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String endBand = " ";

        String str = "SCELL: UARFCN 4384, Prim. SC: 362, RSCP: 70, EcNo: 44";

        endBand = getBandNumber(str);

        System.out.println("endBand is " + endBand);

    }

    // ************************************************
    // Returns the current band that the device is on.
    // Currently only coded for 3G
    // ************************************************
    private static String getBandNumber(String str) {

        // The string returned to str will be in the form of:
        // "SCELL: UARFCN  4384, Prim. SC: 362, RSCP: 73, EcNo: 33"
        // ^^^^
        // String str = read_AT("AT+XL1SET=\"IRATSCEL?\"", 10);

        String band = " ";
        int begin = 0, end = 0;

        // Filter through the string to extrace the channel number
        for (int i = 0; i < str.length(); i++) {

            char c = str.charAt(i);

            if (c == 'N' && str.charAt(i + 1) == ' ') {

                begin = i + 1;

            } else if (c == ',') {

                end = i;

                break;

            }

        }

        band = str.substring(begin, end);
        System.out.println("band is " + band);

        if (band.equals("4384")) {

            band = "5";

        } else {

            band = "2";
        }

        return band;

    }

}
+4
source share
3 answers

You have a space in your range variable before 4384. Try typing like this:

System.out.println("band is '" + band + "'");
+6
source

After the assessment, you get to the line that is actaully eqaul before " 4384"(note the space).

...

if (band.trim().equals("4384")) {...

+7

:

 // Filter through the string to extrace the channel number
    for (int i = 0; i < str.length(); i++) {

        char c = str.charAt(i);

        if (c == 'N' && str.charAt(i + 1) == ' ') {

            begin = i + 2; //i + 2

        } else if (c == ',') {

            end = i;

            break;

        }

    }
+1

All Articles