2

I want to get a specific router info out of a WIFI scan in processing. First I scan for the WIFI and save it into a list.

WifiManager wifiManager = (WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE); 
    wifiManager.startScan();

   List<ScanResult> wifiList = wifiManager.getScanResults();

then I check if any of the items equals to the router name I am searching for (" dlink").

for(int i = 0; i < wifiList.size();i++){

if( wifiList.get(i).SSID.toString() == "dlink"){
         con[0] = "router1 found";
}
}

The output is just null. I know for a fact that the router is in there. I printed out the list, checked in which position of the wifiList the router is. Compared the SSID of that router from the list to the router name, but it didn't work. I printed out the router's name char by char and it was the same.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
jakic
  • 23
  • 7
  • @c3st7n Please notice the [tag:processing] tag. See also: [Processing != Java](https://meta.stackoverflow.com/questions/321127/processing-java) – Kevin Workman Nov 07 '17 at 17:12

1 Answers1

1

You should almost never use == to compare String values. You should use the equals() function instead.

From the reference:

To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

See also: How do I compare strings in Java?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thank you so much, really i cant thank you enough, have a great day! – jakic Nov 07 '17 at 17:11
  • @jakic No problem. See also: https://stackoverflow.com/help/someone-answers and https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Kevin Workman Nov 07 '17 at 17:13