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 3 of 3

Thread: How to show message which abbrevate week start letter to its full name of the day in java?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to show message which abbrevate week start letter to its full name of the day in java?

    Basically i've been trying to create a program which when you enter abbreviations for days it pops up with the full name of the day, e.g M = monday, Tu = tuesday and so forth. Sounds simple enough but i've spent couple of hours constantly making changes to the code and have made a little progress being that there is no compiling error. But the current problem i have now is that the show message dialog box is not appearing after i have entered the letter so i was wondering if any of you guys could see where i've gone wrong:

    import javax.swing.JOptionPane;
    public class DaysOfWeek
    {
      public static void main(String[]args)
      {
    	  String input1 = JOptionPane.showInputDialog("Enter the abbreviation for a day");
    	  String day = (input1);
    	  if (day == "M")
    	  {
    	  JOptionPane.showMessageDialog(null,"Monday");
          }
          else if (day == "Tu")
          {
    	  JOptionPane.showMessageDialog(null,"Tuesday");
          }
      }
    }
    Last edited by Deep_4; November 7th, 2012 at 12:41 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: If statement problem

    Never compare Strings or other objects with ==, use the equals method instead.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: If statement problem

    Yes we must use equals method.
    When a user inputs the abbreviation the String is constructed with the new operator and hence new String object is created. This "==" checks if both the references are pointing to the same object. For example in this case if user inputs the abbreviation as M, then a String M is created using new operator. Now when we compare --> if (day == "M"). Now this second "M" is actually another object which resides in string pool constant. As the objects are different the "if " would result in false and hence would not work. So use "equals" method

Similar Threads

  1. if....else statement
    By Appu14 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 2nd, 2012, 06:35 AM
  2. Need help with if statement.
    By dreamer in forum Loops & Control Statements
    Replies: 2
    Last Post: May 30th, 2012, 02:27 PM
  3. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM
  4. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  5. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM