Results 1 to 9 of 9
  1. #1
    Mr U is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default 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!]");
    
    	}
    	
    	
    	
    	  }
    	
    	  }

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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 :)

  3. #3
    Mr U is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default 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

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

    Default Re: Issues with if else and JOptionPane

    Quote Originally Posted by Mr U View Post
    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.

  5. #5
    Mr U is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Issues with if else and JOptionPane

    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!]");
    
    	}
    	
    	
    	
    	  }
    	
    	  }
    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.

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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!]");
     
        }
         
         
         
          }
         
          }

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

    Default Re: Issues with if else and JOptionPane

    Quote Originally Posted by Addez View Post
    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,
    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(...).

  8. #8
    Mr U is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Issues with if else and JOptionPane

    I'm still having the error, I'm really not too sure on how to fix it :'(

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

    Default Re: Issues with if else and JOptionPane

    Quote Originally Posted by Mr U View Post
    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.

Similar Threads

  1. JOptionPane Help
    By adjit in forum New To Java
    Replies: 23
    Last Post: 01-04-2012, 02:20 PM
  2. Help with JOptionPane
    By javaman1 in forum New To Java
    Replies: 6
    Last Post: 11-13-2010, 10:40 PM
  3. JOptionPane
    By hfbroady in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2010, 11:07 AM
  4. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 09:40 PM
  5. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM

Posting Permissions

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