• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The "number guessing game"

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


See notes in code.
reply
    Bookmark Topic Watch Topic
  • New Topic