1

I'm a java beginner and I'm reading First head Java book. I did manage what the book asks for, but I'm trying to improve the program

the program is supposed to generate a random number between 0-7, then put it an an array with the following two number and the user has to guess it. everything is working except that I'm trying to restrict the user from entering anything but numbers from 0 to 7

here is my try, and I don't know why it's not working:

public String  checkYourSelf (String stringGuess){
    String result = "miss"; //this will be returned if the guess doesn't match
    if (stringGuess != "0" ||
        stringGuess != "1" ||
        stringGuess != "2" ||
        stringGuess != "3" ||
        stringGuess != "4" ||
        stringGuess != "5" ||
        stringGuess != "6" ||
        stringGuess != "7" ){
            System.out.println("Please Enter a correct number");
            result = "error";
            return result;
        } 

and after this I use the parseInt method

int guess = Integer.parseInt(stringGuess);

the whole method :

    public String  checkYourSelf (String stringGuess){
    String result = "miss"; // this will be returned if the guess doesn't match
    if (stringGuess != "0" ||
        stringGuess != "1" ||
        stringGuess != "2" ||
        stringGuess != "3" ||
        stringGuess != "4" ||
        stringGuess != "5" ||
        stringGuess != "6" ||
        stringGuess != "7" ){
            System.out.println("Please Enter a correct number");
            result = "error";
            return result;
        } 
    int guess = Integer.parseInt(stringGuess);

    for (int cell : cellsLocations) {
        if (enterdNums.contains(guess)){
            result = "error";
            System.out.println ("You've entered this number already.");
            System.out.println("Please try again");
            break;
        }
        if (cell == guess) {
            result = "hit";
            hits++;      
            break;
        }
    }
    if (hits == cellsLocations.length) {
        result = "kill"; 
    }
    if (result != "error"){ 
        System.out.println(result);
    }
    enterdNums.add(guess); // add to the list so it doesnt
    return result;   
}
    }

in the main method :

while (isAlive) {
        guess = input.getUserInput("Enter a number");
        result = dot.checkYourSelf(guess);
        if (result == "error"){
            continue;
            } 
        numOfGuesses++;

        if (result == "kill"){
            System.out.println(numOfGuesses);
            isAlive = false;
            break;
        } 
    }

I know there might be other ways to do this, but I'm trying to do it from stuff I know already, and logically this should be working, however if I enter any number it will say "Please enter a correct number"

thanks

Yasser Altamimi
  • 429
  • 5
  • 13

3 Answers3

0

Regarding the problem you have: You never compare strings using !=, because String is an Object, not primitive. Use string' equals() method instead.

A piece of advice: Because you test against only certain chars, you can use very simple regex which is shorter and more understandable than the construction you have:

String regex = "^[0-7]{1}$";

where [0-7] - allowed chars and {1} - string length

In your condition you put:

if (!stringGuess.matches(regex))
Battle_Slug
  • 2,055
  • 1
  • 34
  • 60
0

use a while loop. Its simple and easy to implement

guess = input.getUserInput("Enter a number");
while(guess < 0 || guess > 7){
    System.out.println("that number is invalid! Enter a number 0-7 : ");
    guess = input.getUserInput("Enter a number");
}

The loop will iterate until the user enters a number between 0-7

Ryan
  • 1,972
  • 2
  • 23
  • 36
  • What does it have with the question? – Battle_Slug Feb 17 '17 at 23:55
  • "I'm trying to restrict the user from entering anything but numbers from 0 to 7" – Ryan Feb 17 '17 at 23:55
  • The question is that String comparison doesn't work for the author. Your answer has nothing with String comparison and thus misleading and just will not work for the author (will not solve their problem). – Battle_Slug Feb 18 '17 at 00:02
  • This method of getting Input is almost identical to his structure minus the fact that he tested each number in between 0-7. Yeah he tests for strings wrong but this is a much better way of solving his problem – Ryan Feb 18 '17 at 00:04
  • Let me repeat: you answer the question that wasn't asked, and your question doesn't solve the author's problem. You better to put it as a comment, not as an answer. – Battle_Slug Feb 18 '17 at 00:06
-1

You need to use method equalsIgnoreCase, that would be:

if (!stringGuess.equalsIgnoreCase("0"))

and so on

Steven
  • 1,236
  • 1
  • 13
  • 37
  • Can someone explain the downvote? – Steven Feb 17 '17 at 23:32
  • This question has been asked a million times and it's already flagged as a duplicate. Your answer doesn't contribute anything new. And `equalsIgnoreCase()` is irrelevant when comparing numbers. – shmosel Feb 17 '17 at 23:33
  • He is comparing strings and then casting into integer, may not be that efficient, but it's the right answer for this case – Steven Feb 17 '17 at 23:38
  • I didn't say it's wrong per se, just not useful and misleading. – shmosel Feb 17 '17 at 23:39
  • Missleading how? It's the right answer for this case scenario, the user asking is obviously still learning, give him some time to get a grip of the concepts, we all need a little help now and then – Steven Feb 17 '17 at 23:40
  • I explained already. Misleading because you're implying case sensitivity is somehow relevant to the problem. – shmosel Feb 17 '17 at 23:41
  • Really? You implying you downvote me because I use equalsIgnoreCase instead of equals? You must be kidding me – Steven Feb 17 '17 at 23:42
  • I downvoted because of the overall quality of your answer. Relevance, accuracy and clarity were all factors. Anyway, you shouldn't take downvotes so personally. It's just one guy expressing his opinion. – shmosel Feb 17 '17 at 23:46
  • You are interpreting that I take this personally, all I'm doing is asking for a decent explanation, and I haven't got it. The question needed no further details as per why the user wasn't getting the result he needed, pointing him in the right direction was all relevance, accuracy and clarity the answer needed. But hey, just a guy's opinion, I'm done with this. – Steven Feb 17 '17 at 23:52