Landscape
Landscape

Reputation: 257

Java: Code ain't working the right way

First you have to know that I'm a noob to Java, i just started to code and it's my first programming language. So please don't be angry if I'm begin a bit stupid, I just wanna learn to code - Thanks

I'm trying to make a simple " guessing game ", but my code aren't waiting for the user-input. please help me, I don't know what to do.

My code:

    public static void main(String[] args)
    {
        //Creating the scanner
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);


        //Creating the two random numbers.
        Random rand = new Random();
        int userNumber  = rand.nextInt(10) + 1;
        int comNumber = rand.nextInt(10) + 1;


         //Asks the user what to do.
        System.out.println("Your number is: " + userNumber +" of 10");
        System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");


            //Checking if the user is right.

        //If the user types in LOWER
        if(userNumber < comNumber && input.equals("L"))
            System.out.println("You are right. The computer's number is: " + comNumber);
        if(userNumber < comNumber && !input.equals("L"))
            System.out.println("You are wrong. The computer's number is: " + comNumber);

        //If the user types in EQUAL TO.
        if(userNumber == comNumber && input.equals("E"))
            System.out.println("You are right. The computer's number is: " + comNumber);
        if(userNumber == comNumber && !input.equals("E"))
            System.out.println("You are wrong. The computer's number is: " + comNumber);

        //If the user types in HEIGHER.
        if(userNumber > comNumber && input.equals("H"))
            System.out.println("You are right. The computer's number is: " + comNumber);
        if(userNumber > comNumber && !input.equals("H"))
            System.out.println("You are wrong. The computer's number is: " + comNumber);

        else
            System.out.println("You can only type in ' L ', ' E ' or ' H '.");




    }
}

I would be happy if you could help me out whit my problem, and tell me how I can remove the @SuppressWarnings("resource") / explain why it have to be there.

Upvotes: 0

Views: 165

Answers (5)

Kristof L
Kristof L

Reputation: 36

You might want to read the documentation of the Scanner class. It has some examples on how to use it properly.

The main problem is in your if statements: With input.equals("L") you are asking if the scanner object is equal to the string "L", which is impossible, since they aren't the same type.

To get a string from the input stream you could use input.next() and compare that to "L". Remember though to only call it once before all the ifs, otherwise the programm waits for a new input at every condition check.

Regarding the warning: As you can read in the documentation, Scanner needs to be closed with input.close() after use.

And just as a hint, never use the @SuppressWarnings annotation if you don't know what your are suppressing. It is meant as a tool for you to use when you know that something the compiler warns you about cannot happen. In this case it tried to warn you about a resource leak, which is absolutely correct.

EDIT:

Just as an idea how you could improve your design. You could do something similar to:

String expectedInput;
if (userNumber < comNumber) {
    expectedInput = "L";
} else if (userNumber == comNumber) {
    expectedInput = "E";
} else {
    expectedInput = "H";
}

String userInput = input.next();
if (userInput.equals(expectedInput)) {
   System.out.println("You are right. The computer's number is: " + comNumber);
} else {
   System.out.println("You are wrong. The computer's number is: " + comNumber);
}

You would still have to check for wrong input though.

This has a more natural flow to the logic and is easier to read.

Another advantage would be that you are separating two different concepts: finding out who had the higher number and finding out if the user guessed right. This might seem like a little thing now, but things like the seperation of concepts and levels of abstraction will become more important the more complex your software gets.

Just had another idea. You could even get rid of the code duplication in the last bit and add a check for wrong input like this:

String userInput = input.next();
boolean isInputValid = Arrays.asList("L", "E", "H").contains(userInput);

if (isInputValid) {
    String rightWrong = userInput.equals(expectedInput) ? "right" : "wrong";
    System.out.println("You are " + rightWrong + ". The computer's number is: " + comNumber);

} else {
    System.out.println("You can only type in ' L ', ' E ' or ' H '.");          
}

Though it is debatable if using a ternary operator is good style...

Upvotes: 0

Ryan Jackman
Ryan Jackman

Reputation: 780

To answer your original question, you are getting an error because you aren't importing java.util.Scanner

import java.util.Scanner;

To address some of the other issues:

You are not reading in anything from the user. You use the Scanner methods to do this.

String guess = input.nextLine() is what I would use.

Secondly, your if statements would not work the way you are using them. This may not be the most compact but it is good for readability.

    if(guess.equals("L")){
        if(userNumber < comNumber){
            System.out.println("You are right. The computer's number is: " + comNumber);
        } else {
            System.out.println("You are wrong. The computer's number is: " + comNumber);
        }
    } else if(guess.equals("E")){
        if(userNumber == comNumber){
            System.out.println("You are right. The computer's number is: " + comNumber);
        } else {
            System.out.println("You are wrong. The computer's number is: " + comNumber);
        }
    } else if(guess.equals("H")){
        if(userNumber > comNumber){
            System.out.println("You are right. The computer's number is: " + comNumber);
        } else {
            System.out.println("You are wrong. The computer's number is: " + comNumber);
        }
    } else {
        System.out.println("You can only type in ' L ', ' E ' or ' H '.");
    }

I also suggest closing the Scanner as good practice at the end. input.close()

Upvotes: 0

Ahmed
Ahmed

Reputation: 2266

You should import java.util.*; And You aren't taking any input from user, try

int inp=input.nextInt();

Upvotes: 0

Kayaman
Kayaman

Reputation: 73558

You're using Scanner wrong. You need to call scanner.nextLine() to get input (a String) from the user, and you need to convert the String to an integer (with Integer.parseInt) to compare it with other ints.

Upvotes: 3

Justin Loveless
Justin Loveless

Reputation: 522

You're going to have to import java.util.Scanner into your project. Take a look at this link that will give you plenty of information about it.

Edit: Just noticed you had it at the top. The next thing you should do is create a variable, say "guess" of type string, and you want to assign input.next() to that variable. Then you can replace the input.equals() functions with the guess.equals() function.

http://www.homeandlearn.co.uk/java/user_input.html

Upvotes: 0

Related Questions