Results 1 to 9 of 9
- 10-12-2012, 12:14 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Issues with if else and JOptionPane
Hello, I seem to be having some issues. Everything run's as in I am getting no error's but the if else will not work. If the statement in the if is correct it still goes to else. No matter what I put in. Can I get some help from you guys?
Thanks!
Java Code:import javax.swing.JOptionPane; public class GUIbasic { public static void main(String[] args) { String fn = JOptionPane.showInputDialog("Enter Here"); // fn is varibles System.out.println(fn); if (fn == "Apple") { JOptionPane.showMessageDialog(null,"[HERE'S Apple!]"); } else { JOptionPane.showMessageDialog(null,"[I didn't see an Apple!]"); } } }
- 10-12-2012, 12:18 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Re: Issues with if else and JOptionPane
fn == "Apple" meens if the String fn is stored at same location as this new String. That is never the case and therefore you need to use fn.equals("Apple") for it to work.
value = 1;
if (value == 1)
This is true. Because 1 is stored at the place one. But as said, in your case you create two different strings stored seperatly and they are therefore not equal.
If they were equal then that would meen that if you changed fn then "Apple" would change aswell because your moddifying same object.
Hope you understand :)
- 10-12-2012, 12:29 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Issues with if else and JOptionPane
I'm sorry I'm not sure I understand you. I tryed replacing fn.equals("Apple") but I got some error's. But really I'm lost after you said "value = 1; if (value == 1) "
Can you explain it a little bit more.
Thank you
-
Re: Issues with if else and JOptionPane
- 10-12-2012, 12:35 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Issues with if else and JOptionPane
the error's are commented out by /* and //Java Code:import javax.swing.JOptionPane; public class GUIbasic { public static void main(String[] args) { String fn = JOptionPane.showInputDialog("Enter Here"); // fn is varibles System.out.println(fn); if fn.equals("Apple") /*Multiple markers at this line - Syntax error on token(s), misplaced construct(s) - fn cannot be resolved to a type - Syntax error on token "if", new expected */ { JOptionPane.showMessageDialog(null,"[HERE'S Apple!]"); } //Syntax error on tokens, delete these tokens else { JOptionPane.showMessageDialog(null,"[I didn't see an Apple!]"); } } }
It was working fine before I added the new code. I'm sure I'm making an error with what he suggested.
- 10-12-2012, 12:39 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Re: Issues with if else and JOptionPane
if (fn.equals("Apple") ){
All if contitions should be placed inside () braces.
Java Code:import javax.swing.JOptionPane; public class GUIbasic { public static void main(String[] args) { String fn = JOptionPane.showInputDialog("Enter Here"); // fn is varibles System.out.println(fn); if ( fn.equals("Apple") ){ { JOptionPane.showMessageDialog(null,"[HERE'S Apple!]"); } //Syntax error on tokens, delete these tokens else { JOptionPane.showMessageDialog(null,"[I didn't see an Apple!]"); } } }
-
Re: Issues with if else and JOptionPane
Your recommendation is correct but your explanation may be a bit off.
The reason the equality operator, == , works for numbers and numeric variables, is because that is how it was defined in the Java Language Specification section 15.21.1 to work and is not because the values are stored in the same location.
For reference types, of which String is one, The JLS section 15.21.3 defines the == operator to return true if the two objects referred to are one and the same, or "object equality". It also states,Note since Strings can be stored on the String pool, the == operator sometimes works for them, but you can't rely on it, so yes, use equals(...) or equalsIgnoreCase(...).While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).
- 10-12-2012, 12:51 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Issues with if else and JOptionPane
I'm still having the error, I'm really not too sure on how to fix it :'(
-
Re: Issues with if else and JOptionPane
Similar Threads
-
JOptionPane Help
By adjit in forum New To JavaReplies: 23Last Post: 01-04-2012, 02:20 PM -
Help with JOptionPane
By javaman1 in forum New To JavaReplies: 6Last Post: 11-13-2010, 10:40 PM -
JOptionPane
By hfbroady in forum AWT / SwingReplies: 3Last Post: 11-12-2010, 11:07 AM -
need help with a JOptionPane
By dr4g0nk1ng in forum Advanced JavaReplies: 2Last Post: 02-19-2010, 09:40 PM -
JOptionpane
By tommyyyy in forum New To JavaReplies: 2Last Post: 03-20-2009, 08:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks