If statement is ignored

Posted by user2898120 on Stack Overflow See other posts from Stack Overflow or by user2898120
Published on 2013-10-19T15:42:43Z Indexed on 2013/10/19 15:53 UTC
Read the original article Hit count: 320

Filed under:
|
|

I am making a simple matchmaker as a learning project in JAVA. My program so far just asks a few questions, but I wanted to do gender specific questions, so I asked for their sex (m or f) and then attempted to add a message that only showed if sex was m. The dialog should say "well done, you are male!". Else it restarts method. Every time, no matter what I type it restarts the program.

Here is my code:

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args){
        setVars();
    }

    public static void setVars(){
        String name = JOptionPane.showInputDialog(null, "What is your name?");
        String sAge = JOptionPane.showInputDialog(null, "What is your age?");
        String sex = JOptionPane.showInputDialog(null, "What is your sex?\n(Enter  m or f)");

        if (sex == "m"){
            JOptionPane.showMessageDialog(null, "Well done, you are male.\nKeep Going!");
        }
        int age = Integer.parseInt(sAge);
        String chars = JOptionPane.showInputDialog(null, "Name three charectaristics");
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about if-statement