2

I wrote a simple if / else that is supposed to print the answer to the if else. but does not respond even with the correct input. I can't see what I'm missing.

import java.util.Scanner;
public class MarriageQuiz{
    public static void main(String[] args){   
        Scanner input = new Scanner(System.in);
        String marStat;
        System.out.print("Please enter your Marital Status (M or S) >> ");
        marStat = input.nextLine();
        marStat = marStat.toUppercase();            
        if(marStat.equals('M')){
            System.out.print("You are married");
        }
        else if(marStat.equals('S')){
            System.out.print("You are single");
        }
    }         
}
5
  • 1
    You need to compare marStat, a string, against another string, i.e. use if (marStat.equals("M")) { ...} Commented Aug 20, 2016 at 7:58
  • So I need to establish a second string then compare it? I'm not sure I understand. Commented Aug 20, 2016 at 8:02
  • 2
    In Java, single quotes are a char and double quotes are a String. You are comparing a String to a char which will always be false. Commented Aug 20, 2016 at 8:06
  • Thanks for telling me the why. It actually explains a lot as I started out with char in the code but found I didn't need it. I forgot to adjust the quote marks. Commented Aug 20, 2016 at 8:13
  • The above is correct, but to add a bit more detail: in Java, chars (single characters enclosed in single quotes) are stored as primitives. Strings are objects. When you call marStat.equals('S'), you are attempting to compare a String object to a char primitive. Under the hood, Java will autobox the char primitive to a Character object, which will fail comparison with a String. I'll add some tips in an answer below. Commented Aug 20, 2016 at 15:07

4 Answers 4

4

Your code is comparing a String object against a character literal, which I believe the JVM will box into a Character object. Well, these two objects don't belong to the same class, so "M".equals('M') will return false. To remedy this, use "M".equals("M").

change toUppercase() to toUpperCase() and marStat.equals('M') to marStat.equals("M") also marStat.equals('S') to marStat.equals("S")

import java.util.Scanner;

public class MarriageQuiz {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String marStat = "";

        System.out.print("Please enter your Marital Status (M or S) >> ");
        marStat = input.nextLine();
        marStat = marStat.toUpperCase();

        if (marStat.equals("M")) {
            System.out.print("You are married");
        } else if (marStat.equals("S")) {
            System.out.print("You are single");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed a problem I didn't know I had yet, thanks.
4

On the other hand, you can use Character type instead of 'String'. Rather using Character would be more accurate as you are dealing with only one character.

Scanner input = new Scanner(System.in);

Character marStat;
System.out.print("Please enter your Marital Status (M or S) >> ");

marStat = input.next().charAt(0);
marStat = Character.toUpperCase(marStat);

if (marStat.equals('M')) {
    System.out.println("You are married");
} else if (marStat.equals('S')) {
    System.out.println("You are single");
}

1 Comment

Please note that although this is not really relevant to this particular case, Character.toUpperCase is not in general a good idea. See the specification: "In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot."
2

use ""

if(marStat.equals("M")){
        System.out.print("You are married");
    }
    else if(marStat.equals("S")){
        System.out.print("You are single");
    }

Comments

0

As mentioned in a comment above, you are comparing a String object to an autoboxed Character object. One fix is obviously using double quotes, which Java will autobox to a String object your code will work.

A few tips to save a few lines of code: use String.equalsIgnoreCase() to save a line converting the incoming string to uppercase.

Next, consider using a constant for marital status:

public class MarriageQuiz{
    private static final String STATUS_MARRIED = "M";
    ...
    if (marStat.equalsIgnoreCase(STATUS_MARRIED)) {
    ...

That way you can use STATUS_MARRIED all over your code but can change it from, say, "M" to "Married" easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.