Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with import java.util.*; dont know whats wrong

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with import java.util.*; dont know whats wrong

    I just started learning java and i made this small game. here is the code

    <import java.util.*;
     
    class GuessingGame 
    {
     public static void main (String []args)
     {
     System.out.println("Welcome to the GUESSING GAME"); 
     
     
     Scanner scan = new Scanner(System.in);
     Random rand = new Random();
     int guess = 0;
     int round = 1; 
     int score = 0;
     String rating="";
     
     while (round <= 10)
      {
      int think = rand.nextInt(10)+1;
      int tries = 1;
      System.out.println("Round:"+round);
      System.out.println();
      System.out.println("I am thinking of a number from 1 to 10.");
      System.out.println("You must guess what it is in three tries.");
      System.out.println("Enter a guess:");
     
     while (tries < 4 )
      {
       guess = scan.nextInt();
     
       if (guess == think)
        {
        System.out.println("RIGHT!");
        System.out.println("You have won this round.");
        System.out.println();
        score = score + 1;
        tries = tries + 3;
        }
       else 
        {
         if (guess == think + 1 || guess == think -1)
           {
           System.out.println("hot");
           }
     
         if (guess == think + 2 || guess == think -2)
           {
           System.out.println("warm");
           }
          else
           {
            System.out.println("cold");
           }
         }
        tries = tries + 1;
       }
     if (guess != think)
     {
     System.out.println("Wrong");
     System.out.println("The correct number was "+think+".");
     System.out.println("You have lost this round");
     }
     
      round = round + 1;
      }
     System.out.println();
     System.out.println("Game over!");
     System.out.println("You have won "+score+" out of 10 rounds");
     if (score <= 7 ){rating = new String ("Amateurs");}
     if (score == 8 ){rating = new String ("Advanced");}
     if (score == 9 ){rating = new String ("Professionals");}
     if (score == 10){rating = new String("Hackers");}
     System.out.println("Your rating: "+rating);
     }
    }>


    WHen i compile it in cmd i get this error message:
    "guessinggame.java:11: error: cannot access Scanner
    Scanner scan = new Scanner(System.in);
    ^
    bad source file: .\Scanner.java
    file does not contain class Scanner
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
    1 error
    "

    when i change the import to "import java.util.Scanner; & import java.util.Random;"
    it works
    WHy does it not work with import java.util.*;??
    PLS HELP

    (BTW in the game itself are maybe some bugs .. i have not tested it yet... my problem is the java util thing)
    Last edited by TheLeader; August 3rd, 2012 at 12:55 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Problem with import java.util.*; dont know whats wrong

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    bad source file: .\Scanner.java
    Do you have a file named Scanner.java? That could confuse the compiler.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with import java.util.*; dont know whats wrong

    OMG it really helped ... i found two files: named Scanner.java and SCANNER.java from some other excercise i did
    i renamed it and now everything is fine again .

    THANKS FOR YOUR HELP

    Could you please explain my shortly why the java compiler had the problem with util.*;
    and with util.Scanner; not ??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Problem with import java.util.*; dont know whats wrong

    I think with the full path to Scanner it used that version, but with the * it used your version of Scanner.

    In general it is a bad idea to name your class the same as a Java SE class.
    Add a prefix like My to make the class name unique.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    TheLeader (August 3rd, 2012)

  6. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with import java.util.*; dont know whats wrong

    ah ok thank you very much

Similar Threads

  1. Whats wrong with my java code? I really need help!
    By matthewpipie in forum What's Wrong With My Code?
    Replies: 18
    Last Post: February 18th, 2012, 07:05 PM
  2. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 09:25 AM
  3. Dont know whats happening
    By JJTierney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2010, 12:50 PM
  4. Something wrong with the import things
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 25th, 2010, 08:30 AM
  5. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM

Tags for this Thread