Results 1 to 2 of 2

Thread: Compiler Error

  1. #1
    CoPilot is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Compiler Error

    I would like some help with fixing an error that i have encountered while trying to compile a java application. This is a short program, and whats needed is a quick fix to one syntax error. Please Help!

    //The error is within the object constructor Rand.

    import java.util.Random;
    import java.util.Scanner;


    //package gg;

    public class GuessingGame {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub


    Random Rand new Random(); //this is the area with the error, i think the Rand object variable needs to be corrected!
    {
    }

    int numberToGuess = Rand.nextInt(1000);
    int numberOfTries = 0;
    Scanner input = new Scanner(System.in);
    int guess;
    boolean win = false;

    while(win == false){


    System.out.println("Guess a number between 1 and 1000:");
    guess = input.nextInt();
    numberOfTries++;

    if (guess == numberToGuess){
    win = true;

    }
    else if(guess < numberToGuess){
    System.out.println("Your guess is to low");
    }
    else if(guess < numberToGuess) {
    System.out.println("Your guess is to low");

    System.out.println("You win!");
    System.out.println("The number was" + numberToGuess);
    System.out.println("It took you" + numberOfTries + "tries");
    }

    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Compiler Error

    Quote Originally Posted by CoPilot View Post
    I would like some help with fixing an error that i have encountered while trying to compile a java application. This is a short program, and whats needed is a quick fix to one syntax error. Please Help!

    Java Code:
    //The error is within the object constructor Rand.
    
    import java.util.Random;
    import java.util.Scanner;
    
    
    //package gg;
    
    public class GuessingGame {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		
    Random Rand new Random();   //this is the area with the error, i think the Rand object variable needs to be corrected!
    {
    }
    
    int numberToGuess = Rand.nextInt(1000);
    int numberOfTries = 0;
    Scanner input = new Scanner(System.in);
    int guess;
    boolean win = false;
    	
    while(win == false){
    
    
    System.out.println("Guess a number between 1 and 1000:");
    guess = input.nextInt();
    numberOfTries++;
    
    if (guess == numberToGuess){
    	win = true;
    
    }
    else if(guess < numberToGuess){
    	System.out.println("Your guess is to low");
    }
    else if(guess < numberToGuess) {
    	System.out.println("Your guess is to low");
    	
    	System.out.println("You win!");
    	System.out.println("The number was" + numberToGuess);
    	System.out.println("It took you" + numberOfTries + "tries");
    }
    
    }
    	}
    }
    First a hearty welcome to java-forums.org! I hope that you find this place helpful and friendly, and that you come by frequently, perhaps initially to ask questions, but as you learn more, then latter to help out others with questions.

    Next some preliminary stuff:
    When posting code, please wrap the code in [code] [/code] tags so that it retains its formatting and is readable. You'll find that the easier it is for others to read your code, the better they'll be able to understand it and the better they'll be able to help you. Also, when posting about compilation errors or run-time exceptions, you'll want to post the actual error messages with your question so we'll be able to understand it better and help better. Next, you'll want to learn and use Java naming conventions and formatting conventions, again so that others can more readily understand your code. Methods and variable names should begin with a lower-case letter and classes with an upper case letter. Careful rigorous use of indentations is essential.

    Now for your problem, you look to be wanting to create a Random object and assign it to a variable. When doing this you will want to use the assignment operator, =. So rather than

    Java Code:
    Random Rand new Random(); 
    {  // these curly braces are not needed
    }
    Do something like:

    Java Code:
    Random rand = new Random();
    Then be sure to use rand as your Random variable, not Rand.

Similar Threads

  1. Compiler error
    By Nikpatel in forum NetBeans
    Replies: 2
    Last Post: 04-26-2012, 09:39 AM
  2. Compiler Error, Please any help?
    By Liberate in forum New To Java
    Replies: 4
    Last Post: 02-04-2012, 07:19 PM
  3. Compiler error please help
    By ShortIt in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 11:11 PM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  5. Compiler Error
    By jeneal in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 01:31 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
  •