Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

The "number guessing game"(JAVA)

Options
  • 18-02-2006 3:33pm
    #1
    Closed Accounts Posts: 15


    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.



    }// end of class

    Thanks in advance for any help/tips


Comments

  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Ok, to understand this, you need to show us your Input.java class - or at least show us the header for that method.

    Hopefully there's a line:
    public static int getInt()

    if not, that's probably your problem. You need some method to read in an int from System.in - by the looks of this, it's an early assignment for you, so those sorts of classes should be given to you.

    anyway, it's impossible to help you fix it unless we see the source for your Input class.

    Aoife.
    P.s. you may want to surround your code with [ code ] and [ /code ] tags (if you remove the spaces.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    This all stems from the unfortunate tendency of a lot of programming course lecturers to write their own (usually bad) UI classes, instead of the perfectly good IO functions supplied.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    ..and authors.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Authors? Really? Oh dear. I support mandatory licensing of people who write alleged authoritative books.


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    rsynnott wrote:
    This all stems from the unfortunate tendency of a lot of programming course lecturers to write their own (usually bad) UI classes, instead of the perfectly good IO functions supplied.

    Ah yes, but that has its own problems entailed.

    In my last first-year, our lecturer started us off with JOptionPane (his predecessor used a UI class from the course book) and all its associated features. To be perfectly honest, I think that bringing in those components as early as he did actually confused students, while one of those classes (which wasn't too bad, looking back now) can really make things easy for the student until they know what they're doing and can actually tell you what the difference is between an int and a float and a String (which the input class forced us to do - rather than just take a String from JOptionPane and then parse it with the appropriate class).

    To be honest, I think those sorts of classes will help students more in the long run (i.e. just so they can tack down the basics before they have to deal with the 'gory' details), even though it would make it a pain for us here, unless we know that class intimately. Of course, the argument can also be put forward that we're not meant to help with assignments in this way... ;)

    Aoife


  • Advertisement
  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    A college course shouldn't start off using GUI elements, tho. In fact, our course has never touched them AT ALL; most people have, hopefully, figured them out on their own.

    And no, we're not meant to help with this stuff; it's in the charter.


  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    I think easy IO Classes have there place in courses, easing the students into programming etc... I do agree though that "proper" console IO needs to be covered much earlier, in some cases it is not covered at all...


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    You know the lecturers should just start a thread in this forum when they have given a project. So as to keep all the posts together and to see which guys need more help in learning the stuff.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    Hobbes wrote:
    You know the lecturers should just start a thread in this forum when they have given a project. So as to keep all the posts together and to see which guys need more help in learning the stuff.
    :D
    Seriously: A better option is Moodle on a college server + private...

    This thead can pretty much be locked/killed unless the OP has moved on/made an effort,tbh.


  • Registered Users Posts: 1,745 ✭✭✭swiss


    This is going to sound very pedantic, but when you are writing code it is important to follow accepted java conventions. For a start, all variable names should start with a lowercase letter, for example int Guess should be int guess. Also, drop the int from intSecretNumber and just go with secretNumber. The type of the secretNumber should be apparant from its declaration. Don't worry about all of this right away, it's just important to keep in mind because it helps to make your code clear and readable.

    Anyway, as other people have said, theres probably an issue with the Input class, assuming there is one. Try the following fragment for user I/O (for example).
    // add an extra import
    import java.util.Scanner;
    
    // Your code goes here
    Scanner scanner = new Scanner(System.in);
    guess = scanner.readInt();
    
    

    Edit: By the way, the Scanner library is as far as I know only available in the 1.5 release of the java sdk, if you are using an older library you will have to use another way to input data, google DataInputStream for more ideas, or just take in the guess as a parameter from the main method if you're stuck.


  • Advertisement
  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

    (though note that Sun don't always obey it themselves)


Advertisement