Can't import java.util.Scanner
Solution 1
I guess you are using an IDE (like Netbeans or eclipse) which allows you to run the code
even if certain classes
are not compilable
. During the application's runtime, if you access this class
it would lead to this exception.
Solution :- Simply Clean Your Project and Build and Run Then Again.
Solution 2
Make sure you have configured PATH, CLASSPATH and JAVA_HOME variable in system Environment variable.
1) It might refers older version of java then 1.5
or
2) May be not added PATH,CLASSPATH, JAVA_HOME variable there.
BTW Your code is works fine in my Eclipse.
Anonymous
Updated on June 04, 2022Comments
-
Anonymous almost 2 years
I have been trying to create program that lets a user input a number of eggs to buy and calculates the price. I have tried using the scanner method to input the integer, but I can't seem to import the scanner method.
Here's what I have:
package eggsorder; import java.util.Scanner; import java.io.IOException; public class EggsOrder { static final double EGGS_DOZEN = 7.25; static final double EGGS_SINGLE = 0.75; static final int DOZEN_NUMBER = 12; public static void main(String[] args) throws IOException { System.out.println("Enter number of eggs for purchase: "); Scanner enter = new Scanner(System.in); int eggs = enter.nextInt(); System.out.println("You ordered " + eggs + "eggs."); System.out.println("That is " + (eggs / DOZEN_NUMBER) + " dozen eggs at 7.25 per dozen and " + (eggs % DOZEN_NUMBER) + " additional eggs at 0.75 each"); System.out.println("Which is a total price of " + (((eggs % DOZEN_NUMBER) * EGGS_SINGLE) + ((eggs / DOZEN_NUMBER) * EGGS_DOZEN)));
This is the error I get after running:
java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Scanner location: class java.util at eggsorder.EggsOrder.<clinit>(EggsOrder.java:7)
The code works without the scanner method, but it needs to use it.
I have tried using java.util and various other variations but to no avail.
Also, I am using the latest version of NetBeans and java
-
Anonymous about 8 yearsEven after adding that in it still says in can not find the scanner symbol.
-
Anonymous about 8 yearsThis solved it It seemed making a new project entirely and copying the code over solved the problem. It appears that saving the .java file into a different folder, as well as in the source folder, creates the scanner error.
-
Mike about 8 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
-
Rudolf about 8 yearsOh, sorry about that! I'll keep that in mind. Thanks!