Results 1 to 4 of 4
- 04-01-2010, 01:26 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
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?"); } }
-
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.
- 04-01-2010, 01:41 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
-
And another thing, always enclose all if, else blocks, and loops in curly braces. Although this is not causing an error now, it will in the future, believe me.
Similar Threads
-
JOptionPane
By izzy in forum New To JavaReplies: 1Last Post: 03-17-2010, 04:55 PM -
Help with JOptionPane
By sanctishit in forum New To JavaReplies: 1Last Post: 03-10-2010, 10:20 AM -
Help with JOptionPane
By SwEeTAcTioN in forum New To JavaReplies: 12Last Post: 11-03-2009, 04:12 AM -
comparing inputting strings from Joptionpane and if statement
By phil128 in forum New To JavaReplies: 2Last Post: 12-06-2008, 06:54 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks