Results 1 to 4 of 4
  1. #1
    matthemes is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Prob: If-Else statements with String and graphical input dialog

    please do help.

    I'm new to Java and programming and I'm already having problems.

    I have a problem with the if - else statements when I'm using strings. It doesn't actually read my Boolean expression for if and then goes straight to else. I getting frustrated and beginning to loose hope in learning Java. Anyone who knows how to fix this please help and reply.

    Screenshots:
    Prob: If-Else statements with String and graphical input dialog-java.jpg
    Attached Thumbnails Attached Thumbnails Prob: If-Else statements with String and graphical input dialog-java.jpg  

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

    Default Re: Prob: If-Else statements with String and graphical input dialog

    Don't compare Strings for equality with the == operator; use the .equals(...) method and read the API documentation for the String class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    matthemes is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Prob: If-Else statements with String and graphical input dialog

    I tried to read the API documentation for the string but I think I'm dyslexic. All those data not formed into information just flushed me out. It was like learning Chinese in one day. But anyways, I just experimented on my codes and used you ".equal(...)" and it worked...thanks so much! Really really appreciate it!


    Lesson of the day:
    Strings on if-else, use .equals(valueOfStringToBeConsideredTrue).


    :D

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

    Default Re: Prob: If-Else statements with String and graphical input dialog

    Quote Originally Posted by matthemes View Post
    I tried to read the API documentation for the string but I think I'm dyslexic. All those data not formed into information just flushed me out. It was like learning Chinese in one day. But anyways, I just experimented on my codes and used you ".equal(...)" and it worked...thanks so much! Really really appreciate it!


    Lesson of the day:
    Strings on if-else, use .equals(valueOfStringToBeConsideredTrue).
    If you know how Java implements objects and primitives (such as ints and doubles) it's easy: consider variables to be little boxes; primitives are stored in those boxes but for object variables (such as Strings) those boxes only store a pointer to the objects. The objects themselves live on a 'heap' where the garbage collector rules. If you use the == operator it checks what is in those boxes, so it can compare primitives (e.g. 3 == 4 or 3.0 == 4.0) but it only compares pointers for equality, i.e. it can check whether or not two pointers point to the same object and it doesn't check the object(s) content. Two Strings with the same content can be two different objects (only their content is equal) but the == operator doesn't check that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with Int and JOption Input dialog
    By stephanie904 in forum New To Java
    Replies: 1
    Last Post: 02-29-2012, 01:22 AM
  2. Replies: 2
    Last Post: 02-22-2011, 03:07 AM
  3. If statements, input, and strings
    By evolvepwnz in forum New To Java
    Replies: 16
    Last Post: 10-22-2009, 09:58 PM
  4. Replies: 2
    Last Post: 11-22-2008, 05:30 AM
  5. JOptionPane - input dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:09 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
  •