This site requires JavaScript, please enable it in your browser!
Greenfoot back
Greenfoot.Master
Greenfoot.Master wrote ...

2014/2/27

Problem

Greenfoot.Master Greenfoot.Master

2014/2/27

#
Hi, I am currently doing a small project and at the moment I am stuck after putting in a code to "allow you in". My code is below and it seems to come up with error even if the the code that i put in is correct. Please help Code: if(Greenfoot.mouseClicked(null)) { String inputstring1 = JOptionPane.showInputDialog("Insert Code"); if(inputstring1 == "Code") { System.out.println("Well Done"); } else { System.out.println("Error"); } }
davmac davmac

2014/2/27

#
Please use code tags when you post code. The problem is that you are comparing strings using '==', which is the reference equality operator. It checks whether two things refer to the same object. In this case you have two different String objects which both contain the same characters. You should generally compare strings using the .equals(...) method instead:
if(Greenfoot.mouseClicked(null))
{
    String inputstring1 = JOptionPane.showInputDialog("Insert Code");
    if(inputstring1.equals("Code"))
    {
        System.out.println("Well Done");
    }
    else
    {
        System.out.println("Error");
    }
}
Greenfoot.Master Greenfoot.Master

2014/2/28

#
Works perfectly now, thank you very much
You need to login to post a reply.