Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The "number guessing game"

 
John Eire
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Eire
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


See notes in code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic