Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 265
Anonymous Avatar asked Nov 30 '25 23:11

Anonymous


2 Answers

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.

like image 151
Vikrant Kashyap Avatar answered Dec 03 '25 12:12

Vikrant Kashyap


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.

like image 25
Gopal00005 Avatar answered Dec 03 '25 12:12

Gopal00005



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!