LukeeTaylorr
LukeeTaylorr

Reputation: 37

Creating a method to return an integer based on user string input

I am not too great at java, but I'm trying to create a method that takes input from the user and returns a value. However difficulty always returns 0?

chosenDifficulty = userName.next().toUpperCase();

 public int getDifficulty() {
    int difficulty = 0;
    if (chosenDifficulty == "HUMAN") {
            difficulty = 1;
        if (chosenDifficulty == "RANDOM") {
            difficulty = 2;
        }else{
            difficulty = 3;
        }
    }
     return difficulty;
}

Thanks for any help, it may just be because its late, but i really can't figure it out....or maybe its just not possible to do it this way....thanks in advance for any help.

Upvotes: 0

Views: 212

Answers (4)

Kevin Walker
Kevin Walker

Reputation: 116

It may always return 0, for two reasons.

First, you can't perform string equality checks in Java with == and expect consistent results. You almost always want to use the ".equals()" method for checking equality between objects. The exact reasons are explained well here:

http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

Second, you are missing an end-brace.

Below should work:

public int getDifficulty() {
    int difficulty = 0;
    if (chosenDifficulty.equals("HUMAN")) {
        difficulty = 1;
    } else if (chosenDifficulty.equals("RANDOM")) {
        difficulty = 2;
    }else {
        difficulty = 3;
    }
    return difficulty;
}

Upvotes: 4

Eddy Herman
Eddy Herman

Reputation: 21

First of all you you should use the .equals() method instead of == when comparing strings.

if (chosenDifficulty.equals("HUMAN")) {

The second if statement, comparing to "RANDOM", is inside the first. This means it is only used when chosenDifficulty is equal to "HUMAN". As this second if statement has an else condition difficulty would always be set to 3 if the if statement was reached, there is no scenario where difficulty is set to 2. This is because to satisfy the first if statement chosenDifficulty must be "HUMAN".Difficulty woud then be set to 1. It would then be set to 3 as it would not be equal to "RANDOM". Or if the first criteria was not met, chosenDifficulty equal to "HUMAN", difficulty would remain as 0.

I have posted below what i think you wanted. If chosenDifficulty is "HUMAN" the difficulty is 1. If it is "RANDOM" the difficulty is 2. If it is anything else it returns 3.

 public int getDifficulty() {
       int difficulty = 3;
    if (chosenDifficulty.equals("HUMAN")) {
            difficulty = 1;
          }
        if (chosenDifficulty.equals("RANDOM")) {
            difficulty = 2;
        }   
     return difficulty;
  }

Upvotes: 2

S McCrohan
S McCrohan

Reputation: 6693

Kevin's answer is good (to wit: there's a missing close-brace, and String equality comparisons should be done with .equals(), not ==).

But as an additional option: this sort of comparison is a good place for a switch statement, instead of chained if. This assumes you're running in at least Java 7, which added switch statements for strings. Matter of taste. switch() is supposedly a bit more efficient, but I think the deciding factor in most circumstances should be which you find more readable.

public int getDifficulty() {
    int difficulty;
    switch (chosenDifficulty) {
        case "HUMAN":
            difficulty = 1;
            break;
        case "RANDOM":
            difficulty = 2;
            break;
        default:
            difficulty = 3;
    }
    return difficulty;
}

Upvotes: 4

Mohan Raj
Mohan Raj

Reputation: 1132

Instead of casting to UPPERCASE and checking the content. Here the objective is check the content irrespective of case so remove

      chosenDifficulty = userName.next().toUpperCase();

Then use equalsIgnoreCase

      chosenDifficulty.equalIgnoreCase("HUMAN")

So that ignoring the case only content will be matched.

Upvotes: 1

Related Questions