jerhynsoen jerhynsoen - 24 days ago 11
Java Question

Read registry compare value to if statement

I have a working code for reading the registry. My problem is the part where I read the registry and I want to compare some if statements to eventually do stuff depending on the answer. I checked the value, it outputs Windows 7 Professional as the value, my if statement disagrees.

My registry code is from here

public class Regtest {

public static void main(String[] args) {
String value = null;
String os7 = "Windows 7 Professional";
try {
value = Registry.readString (
Registry.HKEY_LOCAL_MACHINE, //HKEY
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", //Key
"ProductName");

if(value == os7 ){
System.out.println("Windows Distribution = " + value);
}else{
if(value != os7 ){
System.out.println("Windows Distribution = Wrong OS");
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

Answer

You need to use value.equals(os7). Java does not support == for Strings. It only checks for object identity which is not a given.