This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The "number guessing game"" Watch "The "number guessing game"" New topic
Author

The "number guessing game"

John Eire
Greenhorn

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:

import java.io.*;



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)
{
System.out.println("CONGRATULATIONS!! YOU GUESSED CORRECTLY!");
}


} //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
Olnex


[url]Olnex.net[/url]
[SCJP 1.2, SCJD, SCWCD]
John Eire
Greenhorn

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"?

thanks!
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
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10


See notes in code.


JavaBeginnersFaq
"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: http://aspose.com/file-tools
 
subject: The "number guessing game"
 
Similar Threads
Help with number guessing game
Incompatible magic value 188741100
compiler telling me to make a file for a public class
Trouble with Guessing Game
what's wrong with the code?