Anonymous
Anonymous

Reputation: 33

Can't import java.util.Scanner

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

Upvotes: 3

Views: 30205

Answers (4)

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

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.

Upvotes: 1

Gopal00005
Gopal00005

Reputation: 2151

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.

Upvotes: 1

Rudolf
Rudolf

Reputation: 1

Try unchecking the "Compile on save" setting in Build -> Compiling.

Source: http://forums.netbeans.org/post-45324.html

Upvotes: 0

Jackson Hurt
Jackson Hurt

Reputation: 312

you can try this

java.util.Scanner enter = new  java.util.Scanner(System.in);

i tested your code and it seems to import correctly on my end. Maybe something is wrong with your IDE try resetting it

Upvotes: -1

Related Questions