Problem with import java.util.*; dont know whats wrong
I just started learning java and i made this small game. here is the code
Code java:
<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)
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.
Quote:
bad source file: .\Scanner.java
Do you have a file named Scanner.java? That could confuse the compiler.
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 :D .
THANKS FOR YOUR HELP
Could you please explain my shortly why the java compiler had the problem with util.*;
and with util.Scanner; not ??
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.
Re: Problem with import java.util.*; dont know whats wrong
ah ok thank you very much :D