简体   繁体   中英

Java boolean returns false even though answer should be correct

I'm currently working on a Smartmirror project with a Raspberry Pi Zero W and I need my java program to check the internet connection so that I know wether I can update my weather data. For this method I use the iwgetid command to get the SSID. The problem is the following: I takethe return of the command line and put it into an if block. Even though the return(which I checked is the correct outprint) is correct the boolean returns false as an output.

public boolean checkwifi() throws IOException, InterruptedException{
    ProcessBuilder ps = new ProcessBuilder("iwgetid");
    ps.redirectErrorStream(true);
    Process pr = ps.start();
    BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line;
    line=in.readLine();
    line.trim();

    if(line == "wlan0     ESSID:\"mySSID\"" || line== "wlan0     ESSID:\"mysecondSSID\"" ){
    pr.waitFor();
    in.close();
    return true;    
    }
    else {
        System.err.println(line);
    pr.waitFor();
    in.close();
    return false;

    }
    }

This is the output I get from the console:

    wlan0     ESSID:"mySSID"
    false       

if(line == "wlan0 ESSID:\\"mySSID\\"" || line== "wlan0 ESSID:\\"mysecondSSID\\"" ) compares the reference of line to the string, and they are different.

Use line.equals("wlan0 ESSID:\\"mySSID\\"") || line.equals("wlan0 ESSID:\\"mysecondSSID\\"") line.equals("wlan0 ESSID:\\"mySSID\\"") || line.equals("wlan0 ESSID:\\"mysecondSSID\\"") to compare the actual value of the Strings.

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