File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes The Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The "number guessing game"" Watch "The "number guessing game"" New topic

The "number guessing game"

John Eire

Joined: Feb 18, 2006
Posts: 2
new to java. Have come across plenty links to this program however i'd like to
see my version workin.

Error "int cannot be dereferenced"

My attempt so far:


public class assignment1A {

public static void main(String[] args) {

// declare variables to hold values for the secret number and the users guess.
int intSecretNumber;
int Guess;

// set a value for the secret number
intSecretNumber = 10;

System.out.println("Welcome to the number guessing game!");

// ask the user to type in their guess
System.out.println("Please guess a number: ");

Guess = Input.getInt(); // this is my error line
// get's users guess and assigns it to the variable

// Guess = console.getInt(); another way i tried?

//Check users guess to see if it's right, or too high/too low.
if (Guess == intSecretNumber)

} //end of main method.

Thanks in advance for any help/tips

}// end of class
HaoZhe Xu
Ranch Hand

Joined: Nov 03, 2003
Posts: 222
I guess the "Input" class in your code was provided by the assignment, it handles input from user.

this kind of error can happen if Input.getInt() return an Object other than a variable (primitive type), probably Integer, if this is the case, just change it to:

hope this helps

Yours Sincerely

John Eire

Joined: Feb 18, 2006
Posts: 2
i dont have an input class.
do i have to create this class or is there another way
of accepting the user's "guess"?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there are a number of ways to get user input.
here's one way

the 'input' you get will be a String, so you will need to convert this
to an int, to match the specs of your guess program. if you have problems
with this, look up the Integer class and the methods that return an int.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059

See notes in code.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: The "number guessing game"
It's not a secret anymore!