Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string.equals function for inequality?

This code compares text entered by the user (userText) with a keyword (endProgram). When the user enters the word exit, the program will complete and close.

public static void main(String[] args) {

    String endProgram = "exit";
    String userInput;

    java.util.Scanner input = new java.util.Scanner(System.in);

    do {
        userInput = input.nextLine();
    } while ( !(userInput.equals(endProgram)) );

}

Is there a better way to write the code within the while parenthesis? That is, instead of using !(String.equals(String)) is there another function to compare inequality?

like image 667
kehmoto1 Avatar asked Jul 10 '26 07:07

kehmoto1


1 Answers

Considering that ! is just one character, there's not much point in having a separate inequality test function that would just return the negation of the equality function.
So no.

like image 61
qaphla Avatar answered Jul 11 '26 20:07

qaphla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!