Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: using if statement on string System.getProperty("os.name") not working

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using if statement on string System.getProperty("os.name") not working

    Hello,

    Several things of note:

    1) I'm very new to both coding and this forum

    2) I did a search for os.name in this forum and got nothing.

    Here's my code:
    if (System.getProperty("os.name") == "Mac OS" || System.getProperty("os.name") == "Mac OS X")
    {
    	NewItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.META_MASK));
    	OpenItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.META_MASK));
    	SaveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.META_MASK));
    	QuitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.META_MASK));
    }
    else
    {
    	NewItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    	OpenItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    	SaveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    	QuitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    }

    I did a
    System.out.println(System.getProperty("os.name");
    And it gives me Mac OS X, but it doesn't set the accelerator to meta, instead setting it to control.

    (NewItem, OpenItem, SaveItem, and QuitItem are all JMenuItems)


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: using if statement on string System.getProperty("os.name") not working

    Do not use == when comparing objects (unless you wish to compare the objects are the same references). Use the equals method (eg myString.equals(otherstring) )

    See http://www.javaprogrammingforums.com...html#post18725

  3. #3
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: using if statement on string System.getProperty("os.name") not working

    Your way of comparing strings is wrong

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using if statement on string System.getProperty("os.name") not working

    well *facepalm*

    thanks guys.

Similar Threads

  1. System.out.println("Hello World!")
    By Crixus in forum Member Introductions
    Replies: 1
    Last Post: April 24th, 2011, 10:08 AM
  2. System.out.println ("Hellow");
    By OnionzRingz in forum Member Introductions
    Replies: 1
    Last Post: March 1st, 2011, 04:56 PM
  3. first itteration of "for" statement acts differntly to all the rest, why?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 4
    Last Post: March 1st, 2011, 10:06 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. System.out.println ("Hello world!")
    By ShaunB in forum Member Introductions
    Replies: 0
    Last Post: December 29th, 2009, 04:17 PM