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!
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!]");
}
}
}
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 :)
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
Quote:
Originally Posted by
Mr U
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) "
His recommendation is correct. If you get errors when you try to implement this, please show your new code and your error messages.
Re: Issues with if else and JOptionPane
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!]");
}
}
}
the error's are commented out by /* and //
It was working fine before I added the new code. I'm sure I'm making an error with what he suggested.
Re: Issues with if else and JOptionPane
if (fn.equals("Apple") ){
All if contitions should be placed inside () braces.
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
Quote:
Originally Posted by
Addez
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.
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, Quote:
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).
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(...).
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
Quote:
Originally Posted by
Mr U
I'm still having the error, I'm really not too sure on how to fix it :'(
Addez gave you new recommendations. If you've changed your code and are still having a problem, you have to post the new code and error messages for us to understand what the problem may be. We can't see code not posted.