Java Error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Java Error

  1. #1
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6

    Java Error

    I am working on the code for a small number guess game , the code is as follows:

    import java.util.*;

    public class guessgame
    {
    public static void main(String[] args)
    {
    // Declare variables, setup keyboard input and the
    // random number generator
    int game_number, user_number;
    String continue_pref
    Scanner data_input = new Scanner(System.in);
    Random generate = new Random();

    do
    {
    // Generate game number
    game_number = generate.nextInt(999) + 1;
    // The following line is a debug line, comment out
    // for real game.
    // System.out.printf("Game number:%d%n", game_number);

    // Get users first guess
    System.out.print("The computer has generated a number.");
    do
    {
    System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
    usernumber = data_input.nextint();

    } while ((user_number >= 0) && (user_number <= 1000));

    // While user has not guessed right and does not want to quit
    while ((user_number == game_number) || (user_number != 0))
    {
    if (user_number > game_Number)
    System.out.printf("You need to guess higher%n");
    else
    System.out.printf("You need to guess lower%n");

    // Get users next guess
    do
    {
    System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
    user_number = data_input.nextInt();

    } while ((user_number >= 0) && (user_number <= 1000));
    }

    if (user_number == 0)
    {
    // User has guessed right
    System.out.printf("%nYou guessed correctly, well done.%nDo you want to play again (y/Y)=Yes: ");
    continue_pref = new String(data_input.next());
    }
    else
    {
    // User wants to quit
    continue_pref = new String("No");
    }
    } while (continue_pref.equalsIgnoreCase("N"));
    }
    }

    I keep on getting the following error when I compile it. I have no idea what this error means or how to fix it , can anybody help me ? cheers.

    C:\guessgame.java:11: ';' expected
    Scanner data_input = new Scanner(System.in);

  2. #2
    Join Date
    Aug 2004
    Posts
    46
    You need a semicolon at the end of the following line:
    Code:
    String continue_pref

  3. #3
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6
    Thanks very much!
    I'm new to java so it's all very confusing to me at the moment!

    I have now recieved these errors :

    m:\guessgame.java:27: cannot find symbol
    symbol : variable usernumber
    location: class guessgame
    usernumber = data_input.nextint();
    ^
    m:\guessgame.java:27: cannot find symbol
    symbol : method nextint()
    location: class java.util.Scanner
    usernumber = data_input.nextint();
    ^
    m:\guessgame.java:34: cannot find symbol
    symbol : variable game_Number
    location: class guessgame
    if (user_number > game_Number)

    Could anybody clarify how to fix these ?
    Thanks

  4. #4
    Join Date
    Aug 2004
    Posts
    46
    You haven't declared the "usernumber" variable anywhere. Same thing with "game_Number". Look closely at your code. These are simple typos.

  5. #5
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6
    Ok.
    I'm very new to Java, only been learning it a few days, thanks very much for the advice

  6. #6
    Join Date
    Aug 2004
    Posts
    46
    Quote Originally Posted by TommyEman
    Ok.
    I'm very new to Java, only been learning it a few days, thanks very much for the advice
    You're very welcome. Java's very picky about what you type, so pay close attention to what the compiler tells you. The more you can get comfortable reading those error messages, the easier it will be to troubleshoot your code. I remember how mysterious a stack trace looked to me the first time. Learning how to read and interpret the messages was a real epiphany.

  7. #7
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6
    Hi guys
    Still struggling, had a go at trying to declare them but think I'm using the wrong syntax , can't find any sites either telling me how, can anyone help?
    cheers

  8. #8
    Join Date
    Aug 2004
    Posts
    46
    Use the variables you actually declared. Look at your original code. You declare "user_number" at the start of your class, but you're trying to use the variable "usernumber" (which hasn't been declared) at the point where you get the error. Java can't tell that you meant to say "user_number" instead of "usernumber", so the compiler is telling that it can't recognize what you wrote. Same thing goes for "game_Number", which is not the same thing as "game_number".

    Fix the typos, so you actually use the variables you declare.

  9. #9
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6
    Cheers bud I really appreciate it.
    This error is still troubling me :

    m:\guessgame.java:27: cannot find symbol
    symbol : method nextint()
    location: class java.util.Scanner
    user_number = data_input.nextint();
    ^
    I dont mean to sound like a pain but ive tried looking up the error and no joy, I really should buy a decent Java book or something!
    Could someone explain this error to me and how to fix it?
    cheers

  10. #10
    Join Date
    Aug 2004
    Posts
    46
    At the risk of beating a dead horse, you've misspelled the nextInt() method. That's what the compiler is telling you.

    m:\guessgame.java:27: cannot find symbol
    Translation: I, the compiler, have no idea what you're talking about on line 27 of your guessgame.java file.

    symbol : method nextint()
    Translation: The thing on line 27 of which I have no clue is the "nextint()" method you're trying to use. It doesn't exist.

    location: class java.util.Scanner
    Translation: There is no nextint() method in the Scanner class; I suggest you check your spelling (and possibly the arguments you're passing, but that's not the problem here).

    user_number = data_input.nextint();
    Translation: In case you still don't know where I'm talking about, it's that "nextint()" you've typed right there. You know, the one after the period. The one that doesn't exist in the Scanner class.

    Just to reiterate: Java is picky about what you type. The spelling of methods, variables, classes, keywords, etc., must match exactly. There's no mercy for those who don't pay attention to detail.

    Good luck!
    Last edited by yawmark; 12-08-2005 at 02:40 PM.

  11. #11
    Join Date
    Dec 2005
    Location
    Scotland ,UK
    Posts
    6
    Compiled
    I see what you mean! I'm used to coding in VB, so Java is totally different for me. I will remember this rule and pay alot more attention next time!
    thanks again

Similar Threads

  1. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 12:43 AM
  2. Java error handling question
    By Tim in forum Java
    Replies: 0
    Last Post: 02-22-2002, 03:45 PM
  3. Tomcat and JAVA: No Class Def Found Error.
    By Jose Rodz in forum Java
    Replies: 4
    Last Post: 01-18-2002, 10:13 PM
  4. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 03:53 AM
  5. Re: Error handling in components called from ASP
    By James Barbetti in forum ASP.NET
    Replies: 2
    Last Post: 11-06-2001, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center