0

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");
    }
}
dcaswell
  • 3,137
  • 2
  • 26
  • 25
  • Strings should ALWAYS compared using `equals()` method. – Pradeep Simha Oct 19 '13 at 15:44
  • `"m"` is _not_ the same as `sex`, even if the value of `sex` is `"m"`. String comparisons should be done using `equals()` (as mentioned above), which compares the equalness, not the identity. – thriqon Oct 19 '13 at 16:04

4 Answers4

1

try

if ( "m".equalIgnoreCase(sex))

you should use equals for comparing string value and == for checking their references

upog
  • 4,965
  • 8
  • 42
  • 81
1

Your code should be:

if ("m".equals(sex)) {
  //
}

== compares objects' addresses / references
.equals compares objects' values

zmirc
  • 825
  • 2
  • 11
  • 27
1

In Java, you dont' compare strings with ==, you have to compare them with the equals() method on String. String has two variants of this method: equals() which is case sensitive, and equalsIgnoreCase(), which is case insensitive. In the examples below, you can use either one.

Try this:

if(sex.equalsIgnoreCase("m") {
    ...
}

Or to guard against nulls...

if("m".equalsIgnoreCase(sex)) {
    ...
}
Todd
  • 30,472
  • 11
  • 81
  • 89
0

because String is an object not a data type like int when it comes to compare two Strings it is done by .equals() method:

package example;

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.equals("m")){
            JOptionPane.showMessageDialog(null, "Well done, you are male.\nKeep Going!");
        }
        int age = Integer.parseInt(sAge);
        String chars = JOptionPane.showInputDialog(null, "Name three charectaristics");
    }
}
Ramin Omrani
  • 3,673
  • 8
  • 34
  • 60