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:
- Zero or one
packagedeclarations. - Zero or more
importstatements. - Zero or more top-level type declarations, like
class,enum, orinterface.
From the Java 1.8 Language Specification, Section 7.3: "Compilation Units"]:
CompilationUnitis 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 OverflowThe "import java.util.function cannot be resolved" error - Stack Overflow
Having trouble using import in java - Stack Overflow
Java 8 cannot import java.util.function - Stack Overflow
methods - Can't import java.util.Scanner - Stack Overflow
Per the java.util.function Javadoc,
Since: 1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
I'm new at this. How can you tell what you are running? I'm using Eclipse
To determine your current Java version in eclipse, go to
Help -> About Eclipse -> Installation Details (Button in
lower Left) -> Configuration pane
Look for the line java.specification.version - on my machine that is
java.specification.version=1.8
Or the line java.runtime.version - on my machine that is
java.runtime.version=1.8.0_11-b12
i solved this problem by trying following solution
Project > Properties > Java Build Path
Select Libraries tab
Select JRE System Library
Click Edit button
Choose an alternate JRE (jre1.8.0_20)
Click Finish button
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.
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.
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?
The problem with importing * is that it increases the chance of naming conflicts.
Let's assume that in your program, you have a class called EventListener, since java.util also has a class called EventListener, right the way you have some conflicts to deal with, but you do not even care that java.util.EventListener in this context.
This really can be avoided, simply by not importing * and only importing specific classes that are truly needed.
@PeterPeiGuo is right, besides that, I want to say I don't use import xxx.xx.*, cause as a developer you need to know which class you should use and which class not. You need to understand everything for your code/application. So import xxx.xx.XXX is better from my view.
I'm new to java, I've been seeing import java.util.* across the web on forums and other sites, I'm curious what does it mean? Is it anything like import java.util.Scanner or Random, etc?
import must be spelt with a lower-case "i". Try this!
...additionally, you might be wanting to say "import java.util.*;" because util is a package that contains many objects. The * tells it to import all of those objects!
java.util is the collections framework package name. you need to specify what classes you want to import so you can refer to them using there unqualified names, makes code more readable.
if you want to import all classes in the package use import java.util.*;
if you need to add third party packages to your project, you can use a dependency management tool like maven and this can be easily integrated with eclipse.
The import goes here:
import java.util.Scanner;
public class JavaApplication2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
It is not a property of the class but a reference the compiler needs to be able to tell what "Scanner" (in this context) refers to. In this case it states that Scanner is defined in java.util which is part of the Java Runtime Environment (JRE).
Furthermore as you go on coding you should structure your files into folders.
As soon as you do so you will have to add this type of line in the first line of the file: package folderName.folderName0.
If you want to use one of your classes from another one you will have to add an import like: import folderName.folderName0.JavaApplication2
You've put an entire java source file into a main function. Simply do this instead:
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
}
}
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?