Ryan Martin
Ryan Martin

Reputation: 77

Java: if / else not responding even with no compile errors

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

Upvotes: 2

Views: 119

Answers (4)

Nate Vaughan
Nate Vaughan

Reputation: 3839

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.

Upvotes: 0

Shahid
Shahid

Reputation: 2330

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

Upvotes: 4

Nikhil
Nikhil

Reputation: 3711

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

Upvotes: 4

Durgpal Singh
Durgpal Singh

Reputation: 11973

use ""

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

Upvotes: 2

Related Questions