Results 1 to 4 of 4
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Trouble with an "if"

    I am just learning to use preferences, but this just doesn't make sense. I set the preference, as a String, get it back as a String, compare that String to what I know it to be but that comparison fails???
    Java Code:
    		try {
    			Preferences check = Preferences.userRoot().node("/alpha");
    			String value = check.get("save_dir", null);
    			System.out.println("The variable \"value\" is " + value);
    			if ( value == "0"){
    				JOptionPane.showMessageDialog(null, "Show Save Lesson folder dialog.");
    			}
    			else if (check.get("save_dir", null) != null){
    				JOptionPane.showMessageDialog(null, "Here must be a URL.");
    			}
    			else {
    I always get the second JOptionPane saying "Here must be a URL"
    Stack trace is only the one line, "The variable "value" is 0" How can I test that value??

  2. #2
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Trouble with an "if"

    I should have been using integers (int) in the first place... problem solved.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trouble with an "if"

    No it isn't: you shouldn't have tried to compare Strings by using the == operator; you should've used the equals( ... ) method instead; now you have just been lucky because Integers are cached (small Integers at least).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Trouble with an "if"

    Quote Originally Posted by JosAH View Post
    No it isn't: you shouldn't have tried to compare Strings by using the == operator; you should've used the equals( ... ) method instead; now you have just been lucky because Integers are cached (small Integers at least).

    kind regards,

    Jos
    .equals will work much better. Thank you! My intention is to use that preference to store the path for a directory. Using .equals saves me making a second preference with a string value.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Replies: 7
    Last Post: 04-05-2012, 07:15 AM
  4. Replies: 1
    Last Post: 12-09-2011, 02:18 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •