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

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.

This question is related to java string time if-statement user-input

The answer is


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)


Change your || to && so it will only exit if the answer is NEITHER "AM" nor "PM".


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to time

Date to milliseconds and back to date in Swift How to manage Angular2 "expression has changed after it was checked" exception when a component property depends on current datetime how to sort pandas dataframe from one column Convert time.Time to string How to get current time in python and break up into year, month, day, hour, minute? Xcode swift am/pm time to 24 hour format How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time? What does this format means T00:00:00.000Z? How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift? Extract time from moment js object

Examples related to if-statement

How to use *ngIf else? SQL Server IF EXISTS THEN 1 ELSE 2 What is a good practice to check if an environmental variable exists or not? Using OR operator in a jquery if statement R multiple conditions in if statement Syntax for an If statement using a boolean How to have multiple conditions for one if statement in python Ifelse statement in R with multiple conditions If strings starts with in PowerShell Multiple conditions in an IF statement in Excel VBA

Examples related to user-input

Simple InputBox function Asking the user for input until they give a valid response Converting String Array to an Integer Array How to save user input into a variable in html and js Command line input in Python Javascript - User input through HTML input tag to set a Javascript variable? Simulate user input in bash script How to scanf only integer and repeat reading if the user enters non-numeric characters? How to get input from user at runtime Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?