The simple solution, as other people have said, is to split your two public classes into two separate .java files. That will solve both the error you've already encountered and another that you were going to encounter later... but "too many public classes" is not what your Java compiler is complaining about. The real reason you're getting that error is that you can't put import statements after class declarations.

The error message is easy to misread. It seems to be complaining about what you're importing --- probably provoking a response like, "But I am importing a class!". But note where the ^ is pointing --- not at java.util.*, but at the word import:

Calc.java:22: error: class, interface, or enum expected
    import java.util.*;
    ^
1 error

This error is actually complaining about the out-of-place import statement --- it shouldn't be there at all. Once you've declared your first class, it's too late to import anything else. In a roundabout way, it actually says so: By line 22 of Calc.java, nothing can appear at the top level but type declarations --- the "interface, class, or enum" it mentioned --- so encountering a line that starts with import is "unexpected", meaning not allowed.

You can verify this by commenting out the second import (line 22) and compiling. You'll still get errors, but different ones.

As an aside, you are allowed to import java.util.*; more than once. The redundant statements will be ignored... as long as they're in the right place. You can verify this by moving the second import up to around line 3 or 4 and compiling. Again, you'll still get errors, but not about that.

The Correct Order

A single .java file (called a "compilation unit" in the language spec) has the following three parts, which must appear in this order:

  1. Zero or one package declarations.
  2. Zero or more import statements.
  3. Zero or more top-level type declarations, like class, enum, or interface.

From the Java 1.8 Language Specification, Section 7.3: "Compilation Units"]:

CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:

CompilationUnit: [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

A compilation unit consists of three parts, each of which is optional[...]

In case that last phrase made you wonder: Yes, a completely empty .java file will compile without warnings... and without producing any .class files.

Solutions

As has been pointed out by others, you can't have more than one top-level public class or other type declaration in a single file. Unless you're willing to nest one of Calc and NumCalc inside the other, they have to split up into Calc.java and NumCalc.java.

Actually, you could sidestep the problem by reducing one class's visibility to package-default, but that's fragile and not the way Java is generally done. If you tried to use the package-default class in any other .java file, even one in the same package, it would fail to compile because it couldn't find the supposedly-package-visible class --- either Calc would be in the wrongly-named NumCalc.java (not where the compiler will look for it), or NumCalc would be hiding out inside Calc.java.

But why bother with all that? You could combine the two classes into one class very easily, and have a more coherent project. (I'm not sure why they're separate classes in the first place.)

Answer from Kevin J. Chase on Stack Overflow
Top answer
1 of 1
2

The simple solution, as other people have said, is to split your two public classes into two separate .java files. That will solve both the error you've already encountered and another that you were going to encounter later... but "too many public classes" is not what your Java compiler is complaining about. The real reason you're getting that error is that you can't put import statements after class declarations.

The error message is easy to misread. It seems to be complaining about what you're importing --- probably provoking a response like, "But I am importing a class!". But note where the ^ is pointing --- not at java.util.*, but at the word import:

Calc.java:22: error: class, interface, or enum expected
    import java.util.*;
    ^
1 error

This error is actually complaining about the out-of-place import statement --- it shouldn't be there at all. Once you've declared your first class, it's too late to import anything else. In a roundabout way, it actually says so: By line 22 of Calc.java, nothing can appear at the top level but type declarations --- the "interface, class, or enum" it mentioned --- so encountering a line that starts with import is "unexpected", meaning not allowed.

You can verify this by commenting out the second import (line 22) and compiling. You'll still get errors, but different ones.

As an aside, you are allowed to import java.util.*; more than once. The redundant statements will be ignored... as long as they're in the right place. You can verify this by moving the second import up to around line 3 or 4 and compiling. Again, you'll still get errors, but not about that.

The Correct Order

A single .java file (called a "compilation unit" in the language spec) has the following three parts, which must appear in this order:

  1. Zero or one package declarations.
  2. Zero or more import statements.
  3. Zero or more top-level type declarations, like class, enum, or interface.

From the Java 1.8 Language Specification, Section 7.3: "Compilation Units"]:

CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:

CompilationUnit: [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

A compilation unit consists of three parts, each of which is optional[...]

In case that last phrase made you wonder: Yes, a completely empty .java file will compile without warnings... and without producing any .class files.

Solutions

As has been pointed out by others, you can't have more than one top-level public class or other type declaration in a single file. Unless you're willing to nest one of Calc and NumCalc inside the other, they have to split up into Calc.java and NumCalc.java.

Actually, you could sidestep the problem by reducing one class's visibility to package-default, but that's fragile and not the way Java is generally done. If you tried to use the package-default class in any other .java file, even one in the same package, it would fail to compile because it couldn't find the supposedly-package-visible class --- either Calc would be in the wrongly-named NumCalc.java (not where the compiler will look for it), or NumCalc would be hiding out inside Calc.java.

But why bother with all that? You could combine the two classes into one class very easily, and have a more coherent project. (I'm not sure why they're separate classes in the first place.)

🌐
Coderanch
coderanch.com › t › 646246 › java › import-static-java-util-Error
import static java.util.* - Error? [Solved] (Beginning Java forum at Coderanch)
February 18, 2015 - In the following code, I import all classes from java.util package, and it compiles fine. But when I try to static import the same package, it doesn't work. It gives me the error, error: cannot find symbol Why I can't use the second code fragment?
Discussions

The "import java.util.function cannot be resolved" error - Stack Overflow
I'm trying to run this practice script from the standard Oracle Java tutorials. This seems to be a common error and I've used SO resources to make attempts to fix this. I've tried Cleaning the pro... More on stackoverflow.com
🌐 stackoverflow.com
Having trouble using import in java - Stack Overflow
I'm attempting to compile some code starting with the line: import java.util.*; and I keep getting the error: class, interface, or enum expected I've seen on other post that this is because I'm not More on stackoverflow.com
🌐 stackoverflow.com
Java 8 cannot import java.util.function - Stack Overflow
To import the classes in the java.util.function package. More on stackoverflow.com
🌐 stackoverflow.com
methods - Can't import java.util.Scanner - Stack Overflow
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 ... Do you have errors anywhere else in your source code? Does the import statement import java.util.Scanner give you an error? More on stackoverflow.com
🌐 stackoverflow.com
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 310948 › why-cant-i-import-java-util-scanner
Why cant i import java.util.scanner? [SOLVED] | DaniWeb
In the case of java.util, it's also problematic to import the whole package if you're also using java.awt.* because both packages have classes called "List" - you get an import conflict, so if you want to use one or the other List class, you have to use the fully qualified name.
🌐
LabEx
labex.io › tutorials › java-how-to-fix-unresolved-import-error-450916
How to fix unresolved import error | LabEx
This comprehensive tutorial will guide you through understanding, diagnosing, and resolving unresolved import errors, helping you become a more proficient Java developer. In Java, imports are a fundamental mechanism for accessing classes and interfaces defined in different packages. They allow developers to use classes from other packages without specifying their fully qualified names every time. import package.subpackage.ClassName; import java.util.ArrayList; import java.util.*; import static java.lang.Math.PI; graph TD A[Java Standard Library] --> B[java.lang] A --> C[java.util] A --> D[java.io] Using Standard Library Classes ·
Find elsewhere
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-import
Java Scanner import
See how explicit, wildcard, and fully qualified references work when reading console input with Scanner, plus how to resolve missing import errors. ... There are two ways to implement the Java Scanner import: explicitly reference the java.util.Scanner package and class in the import, or do a wildcard import of java.util.*.
Published: February 12, 2026
🌐
Reddit
reddit.com › r/learnprogramming › [java] the import java.util.math cannot be resolved
r/learnprogramming on Reddit: [Java] The import java.util.Math cannot be resolved
February 13, 2014 -

I'm writing a program to calculate the wind chill based on a temperature and wind speed entered by the user. The formula to calculate wind chill is

Wind Chill = 35.74 + 0.6215T - 35.75V0.16 + 0.4275TV0.16

My current code is

import java.util.Scanner;
import java.util.Math;
public class WindChill {

public static void main(String[] args) {
	
	//Declare variables

	double windChill;
	double temperature;
	int    windSpeed;

//Ask the user for temperature and wind speed

System.out.print("Enter the temperature (Fahrenheit):  ");
	Scanner input = new Scanner (System.in);
	temperature = input.nextDouble();

System.out.print("Enter the wind speed(mph):  ");
	windSpeed = input.nextInt();

	
//Display the wind chill value

windChill = (35.74+(0.6215*temperature)) - (Math.pow(35.75*windSpeed,0.16)) + (Math.pow(0.4275*temperature*windSpeed,0.16));	
System.out.print("The wind chill index is " + windChill);
}

}

I get an error in the IDE on line 2 that says "the import java.util.Math could not be resolved". Also, when I try to run the program, I get the error message

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at WindChill.main(WindChill.java:6)

I checked my installed JREs and I am using JRE7. Can anybody tell me what is wrong?

🌐
Bugsly
bugsly.dev › home › blog › fix missing import in java
Fix Missing Import in Java | Bugsly
October 31, 2025 - Java's cannot find symbol or package does not exist errors mean the compiler can't locate a class. This is either a missing import statement or a missing dependency.
🌐
Quora
quora.com › Why-in-Java-is-there-the-line-import-Java-util-*
Why, in Java, is there the line 'import Java.util.*'? - Quora
Answer (1 of 4): import java.util.*; is header to import util package for different classes like Scanner Or Collection framework classes
🌐
Reddit
reddit.com › r/learnprogramming › [java] import java.util.scanner not being recognized?
r/learnprogramming on Reddit: [Java] import java.util.Scanner not being recognized?
January 14, 2018 -

I'm trying to import the scanner class into my program (I'm using NetBeans) but when I type "import java.util.Scanner;" I get an error that says "cannot find symbol: class Scanner"

I thought maybe I typed it wrong so I even copy and pasted the line from code on a different working project and it's still saying that. Not sure where to go from here. Anyone know what could be causing this error?

🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › please fix the error in this coding: import java.io.*; import java.util.scanner; import javax.swing.*; import java.awt.*; import java.util.*; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.objectinputstream; public class helloworld { public static void main(string[] args) { scanner input =
Please fix the Error in this coding: import | Chegg.com
April 30, 2016 - Please fix the Error in this coding: import java.io.*; import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.util.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; public class HelloWorld { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine(); int[] counts = getCharacterFrequency(text); // Count frequency ·