0

I am not sure why I get continuing told the error of java22 in my code. I searched other person's question like around. like here Compiler error: "class, interface, or enum expected"

But I could not find suitable answer for this. It seems like error not only class, interface or enum import part. Some cording has issue it. But but if so I would like to know, which part is course of the problem in this case?

Also, I would like to know the if I want disable to use the num2 for case2. Since it is factorial, it should ask only num1.

Thank you for all the help.

This is the message

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

This is a code part

import java.util.*;
public class Calc
{
public static void main(String[] args)
{
    NumCalc calc = new NumCalc();
    System.out.println("Welcome to Math NumCalc!\nPlease choose an option:" 
    + "\n\t1 " + "- add two real numbers" 
    + "\n\t2 " + "- subtract two real numbers"
    + "\n\t3 " + "- multiply two real numbers"
    + "\n\t4 " + "- devide two real numbers"
    + "\n\t5 " + "- get the factorial of an number"
    + "\n\t6 " + "- menu"
    + "\n\t0 " + "- exit"
    );
    calc.run();
}
}

import java.util.*;
public class NumCalc 
{
private int option = -1;
private Scanner scan, num1, num2;
//private int opt;
double ans  = 0.0;
//int category;

public NumCalc()
{
    scan = new Scanner(System.in); 
}

// entry point for class
public void run()
{
    System.out.println("\n? " + scan);
    int scan = scan.nextInt();

    for (int i=0;i < count; i++)
    {
        option = option + scan.nextInt(); 
    }

    System.out.println("Enter 1st num:");
    int num1 = scan.nextInt();
    System.out.println("Enter 2nd num");
    int num2 = scan.nextInt();

    switch (option)
    {
        case 6:
        ans = num1 + num2;
        System.out.println(num1 + " + " + num2 + "=" + ans);
        break;
        case 5:
        ans = num1 - num2;
        System.out.println(num1 + " - " + num2 + "=" + ans); 
        break;
        case 4:
        ans = num1 * num2;
        System.out.println(num1 + " * " + num2 + "=" + ans);
        break;            
        case 3:
        ans = num1 / num2;
        System.out.println(num1 + " / " + num2 + "=" + ans);
        break;
        case 2:
        num2 = boolean(false); // No 2nd number.
        int ans = 1; 
        for (i = 1;i <= n; i++)
        {
            ans *= n;
        }
        break;
        System.out.println("Factorial of" + num1 + " = ");
        case 1:
        //opt = opt + scan.nextInt();
        continue;
        default:
        close();
    }
}
}
Community
  • 1
  • 1
dukej
  • 37
  • 1
  • 2
  • 5

1 Answers1

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.)

Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43