Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check if a string is not equal to an object or other string value in java?

Tags:

I have been trying to make a clock that the user can set. I wanted the user to be asked questions and they answer in words like yes or no. I have done it for things that don't change using this code such as whether or not the user wants seconds to be displayed or not, but it doesn't work as well when I want the string to change, say from AM to PM when hours exceeds 12. Here is what I am using:

    System.out.println("AM or PM?"); 
    Scanner TimeOfDayQ = new Scanner(System.in);
    TimeOfDayStringQ = TimeOfDayQ.next();

    if(!TimeOfDayStringQ.equals("AM") || !TimeOfDayStringQ.equals("PM")) {
        System.out.println("Sorry, incorrect input.");
        System.exit(1);
    }

    ...

    if(Hours == 13){
        if (TimeOfDayStringQ.equals("AM")) {
            TimeOfDayStringQ.equals("PM");
        } else {
            TimeOfDayStringQ.equals("AM");
        }
                Hours = 1;
        }
     }

Every time I enter anything when it prompts me, whether I put AM, PM, or other wise, it gives me the error I wrote and then exits. When I remove the section of code that terminates the program with the error it will not change the string from AM to PM when hours equals 13. Thank you for your help, it is much appreciated.

like image 736
Pillager225 Avatar asked Oct 16 '11 18:10

Pillager225


People also ask

How do you check if a String is not equal to another String?

Use the strict inequality (! ==) operator to check if two strings are not equal to one another, e.g. a !== b . The strict inequality operator returns true if the strings are not equal and false otherwise.

Can you use != For strings in Java?

Note: When comparing two strings in java, we should not use the == or != operators. These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer.

How do you check if a String equals another String?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

How do you compare objects and strings?

String Comparison With Objects Class The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.


2 Answers

Change your code to:

System.out.println("AM or PM?"); 
Scanner TimeOfDayQ = new Scanner(System.in);
TimeOfDayStringQ = TimeOfDayQ.next();

if(!TimeOfDayStringQ.equals("AM") && !TimeOfDayStringQ.equals("PM")) { // <--
    System.out.println("Sorry, incorrect input.");
    System.exit(1);
}

...

if(Hours == 13){
    if (TimeOfDayStringQ.equals("AM")) {
        TimeOfDayStringQ = "PM"; // <--
    } else {
        TimeOfDayStringQ = "AM"; // <--
    }
            Hours = 1;
    }
 }
like image 105
Eng.Fouad Avatar answered Oct 17 '22 01:10

Eng.Fouad


you'll want to use && to see that it is not equal to "AM" AND not equal to "PM"

if(!TimeOfDayStringQ.equals("AM") && !TimeOfDayStringQ.equals("PM")) {
    System.out.println("Sorry, incorrect input.");
    System.exit(1);
}

to be clear you can also do

if(!(TimeOfDayStringQ.equals("AM") || TimeOfDayStringQ.equals("PM"))){
    System.out.println("Sorry, incorrect input.");
    System.exit(1);
}

to have the not (one or the other) phrase in the code (remember the (silent) brackets)

like image 37
ratchet freak Avatar answered Oct 17 '22 03:10

ratchet freak