Results 1 to 4 of 4
  1. #1
    surg3y3 is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default help with JOptionPane and if/else statement

    i'm trying to write a program in JOptionPane that recognizes a certain command then gives a specified response, but when i try to compile it, it gives an error at the beginning of each else statement, I don't know what's wrong.

    here's the program:

    Java Code:
    import javax.swing.JOptionPane;
    public class jopstring
    {
    
        public static void main(String[] args)
        {
          String cmdString =
          	JOptionPane.showInputDialog("Enter a Command: ");
    
    		if(cmdString == ("Eat some broccoli"));
              JOptionPane.showMessageDialog(null,"Yum!!!") ;
    
    
           else if(cmdString == ("Drink some tea"));
              JOptionPane.showMessageDialog(null,"Wow!!!");
    
    
           else if(cmdString == ("Stop this charade"));
              JOptionPane.showMessageDialog(null,"Bye!") ;
    
           else
              JOptionPane.showMessageDialog(null, "What was that?");
    
    
    
    
    
    
    
    
      }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    To compare Strings, don't use == which tests if one String object is the same as another String object, not if the Strings contain the same content. Instead use the equals(...) method or the equalsIgnoreCase method.

    Best of luck.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Also, don't just throw around parentheses and semi-colons like they're confetti on New Year's Eve. Step through your code again and think carefully about every character, every word, and every piece of punctuation. Ask yourself about each one, "what does this do?" (Hint: if is not a method, but a statement.)

    -Gary-

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JOptionPane
    By izzy in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 05:55 PM
  2. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 11:20 AM
  3. Help with JOptionPane
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 11-03-2009, 05:12 AM
  4. Replies: 2
    Last Post: 12-06-2008, 07:54 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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