r/learnjava • • Mar 09 '14

How can I check if a string variable DOESN'T equal to something?

Hi!

I am trying to use an if-else if statement, but I am having some problems. I understand that for comparing string contents you use:

if (variable.equals("computer")) {
    do stuff
}

But basically, what I am trying to do is the following:

if (variable.equals("computer")) {
    do stuff
} else if (variable.doesntEqual("computer")) {
    do other stuff
} else if (variable doesntEqual("computer")) {
     do more different stuff
}

I am basically trying to check twice (maybe even 3 times) if the variable is indeed what the System has recorded, and each "else if" asks the user to insert it again. If by the 2nd or 3rd time (still debating) it isn't right, the program ends. If it happens to be right the 2nd or 3rd time, it will then move on to the rest of the code.

I've got the breaking in and out of loops figured (took some work, but in the end I managed to do it :D) only that part is the one that I can't quite accomplish. May be something really simple that I am simply overlooking!

Thanks in advance :)

0 Upvotes

3 comments sorted by

10

u/machloj Mar 09 '14

you just need use "not" sign - ! just like that: else if(!variable.equals("computer"){

}

1

u/joaopada Mar 13 '14

Really late answer, but thanks, that worked! :D

1

u/indivisible Mar 10 '14 edited Mar 10 '14

Not a direct answer to your question but an alternative choice for your design. If you're using Java 7 you can use String switches to enable easy expansion in the future and a loop to allow you to not have such hard-coded logic.

String variable;
boolean doBreakForLoop = false;
int numAttemptsAllowed = 3;
for (int i=0; i < numAttemptsAllowed; i++){
    variable = getVariableHowever();
    switch (variable) {
        case "computer":
            doSomething();
            break;                    // this breaks out of the switch
        case "anotherOption":
            doSomethingElse();
            break;                    // this breaks out of the switch
        case "cases can share code":
        case "if the previous cases":
        case "don't break out of the switch":
            runSameCodeForMultipleCases();
            break;
        case "keepAddingMoreCases":
            doMoreStuff();
            doBreakForLoop = true;    // set the boolean to quit the for loop wherever your logic dictates.
            break;                    // this breaks out of the switch
        default:                      // default case runs if no defined cases match the supplied input
            doSomethingIfDoesntMatchAnyDefinedCases();
            break;                    // this breaks out of the switch
    }
    if (doBreakForLoop) {
        break;                        // breaks out of the for loop depending on boolean
    } else {
        system.out.println("Nope, that's not right. Try again.");
    }
}

Just an idea.

Another would be that if you wanted to perform only two actions depending on whether or not the supplied string is or is not one you have defined could be:

String[] knownWords = new String[] {"one", "two", "three", "four"};
List<String> knownWordsList = Arrays.asList(knownWords);
String variable;
int numAllowedAttempts = 3;
for (int i=0; i<numAllowedAttempts; i++) {
    variable = getVariableHowever();
    if (knownWordsList.contains(variable) {
        doSomethingIfItIsAKnownWord();
        // no break here so run loop again to get new input
    } else {
        doSomethingElse();
        break;    // break out of loop and continue with rest of code.
    }
}