-1

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();
            }                                              
        }  
    }
Community
  • 1
  • 1
jerhynsoen
  • 205
  • 1
  • 7
  • 20

1 Answers1

0

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

John Smith
  • 2,282
  • 1
  • 14
  • 22
  • If you *really* want to compare object identity here, you can consider `os7 == value.intern()`; though, if `value` is NOT `"Windows 7 Professional"` you may end up interning it into the string pool which may not be desirable. – obataku Aug 08 '12 at 19:18