jerhynsoen jerhynsoen - 1 year ago 81
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 (
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", //Key

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download