19

I have been troubleshooting this program for hours, trying several configurations, and have had no luck. It has been written in java, and has 33 errors (lowered from 50 before)

Source Code:

/*This program is named derivativeQuiz.java, stored on a network drive I have permission to edit
The actual code starts below this line (with the first import statement) */
import java.util.Random;
import java.Math.*;
import javax.swing.JOptionPane;
public static void derivativeQuiz(String args[])
{
    // a bunch of code
}

The error log (compiled in JCreator):

--------------------Configuration: <Default>--------------------
H:\Derivative quiz\derivativeQuiz.java:4: class, interface, or enum expected
public static void derivativeQuiz(String args[])
              ^
H:\Derivative quiz\derivativeQuiz.java:9: class, interface, or enum expected
    int maxCoef = 15;
    ^
H:\Derivative quiz\derivativeQuiz.java:10: class, interface, or enum expected
    int question = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of questions you wish to test on: "));
    ^
H:\Derivative quiz\derivativeQuiz.java:11: class, interface, or enum expected
    int numExp = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the maximum exponent allowed (up to 5 supported):" ));
    ^
H:\Derivative quiz\derivativeQuiz.java:12: class, interface, or enum expected
    Random random = new Random();
    ^
H:\Derivative quiz\derivativeQuiz.java:13: class, interface, or enum expected
    int coeff;
    ^
H:\Derivative quiz\derivativeQuiz.java:14: class, interface, or enum expected
    String equation = "";
    ^
H:\Derivative quiz\derivativeQuiz.java:15: class, interface, or enum expected
    String deriv = "";
    ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
    ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                   ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                                  ^
H:\Derivative quiz\derivativeQuiz.java:19: class, interface, or enum expected
        deriv = "";
        ^
H:\Derivative quiz\derivativeQuiz.java:20: class, interface, or enum expected
        if(numExp >= 5)
        ^
H:\Derivative quiz\derivativeQuiz.java:23: class, interface, or enum expected
            equation = coeff + "X^5 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:24: class, interface, or enum expected
            deriv = coeff*5 + "X^4 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:25: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:29: class, interface, or enum expected
            equation = equation + coeff + "X^4 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:30: class, interface, or enum expected
            deriv = deriv + coeff*4 + "X^3 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:31: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:35: class, interface, or enum expected
            equation = equation + coeff + "X^3 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:36: class, interface, or enum expected
            deriv = deriv + coeff*3 + "X^2 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:37: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:41: class, interface, or enum expected
            equation = equation + coeff + "X^2 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:42: class, interface, or enum expected
            deriv = deriv + coeff*2 + "X + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:43: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:47: class, interface, or enum expected
            equation = equation + coeff + "X + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:48: class, interface, or enum expected
            deriv = deriv + coeff;
            ^
H:\Derivative quiz\derivativeQuiz.java:49: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:53: class, interface, or enum expected
            equation = equation + coeff;
            ^
H:\Derivative quiz\derivativeQuiz.java:54: class, interface, or enum expected

            if(deriv == "")
            ^
H:\Derivative quiz\derivativeQuiz.java:57: class, interface, or enum expected
            }
            ^
H:\Derivative quiz\derivativeQuiz.java:114: class, interface, or enum expected
    JOptionPane.showMessageDialog(null, "Question " + z + "\\" + question + "\nDerivative: " + deriv);
    ^
H:\Derivative quiz\derivativeQuiz.java:115: class, interface, or enum expected
    }
    ^
33 errors

Process completed.

I feel like this is a basic error, and yet I can't seem to find it. If it makes a difference, I am using JCreator to compile and everything is installed correctly.

UPDATE: I have fixed the errors involved (Class declaration and incorrect import statements (someone went back and deleted a few semicolons))

Working code:

import java.util.Random;
import javax.swing.JOptionPane;
import java.lang.String;
public class derivativeQuiz_source{
public static void main(String args[])
{
    //a bunch more code
}
}

Thanks for all the help

Azulflame
  • 1,534
  • 2
  • 15
  • 30
  • For everyone's information, this error pops out if you call import statements over package declaration too (I know its dumb), just sayin', I spent half an hour figuring it out. – yobro97 Sep 28 '18 at 06:21
  • It can be an encoding problem. check this: https://stackoverflow.com/questions/31665663/android-studio-error-class-interface-or-enum-expeted/53350503#53350503 – c-an Nov 17 '18 at 10:57
  • This error is hard to find sometimes a extra or less brace } is the reason for this error. – Trishant Saxena Jul 27 '23 at 20:39

7 Answers7

30

You miss the class declaration.

public class DerivativeQuiz{
   public static void derivativeQuiz(String args[]){ ... }
}
azendh
  • 921
  • 8
  • 23
  • FYI this error also happens if there is extraneous text in the java file such as pasting in extra stuff from a code sample page. I learned this the hard way lol. – jrdevdba Apr 12 '18 at 15:41
9

Every method should be within a class. Your method derivativeQuiz is outside a class.

public class ClassName {

 ///your methods
}
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
4

You forgot your class declaration:

public class MyClass {
...
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
jalynn2
  • 6,397
  • 2
  • 16
  • 15
0

Look at your function s definition. If you forget using "()" after function declaration somewhere, you ll get plenty of errors with the same format:

 ... ??: class, interface, or enum expected ...

And also you have forgot closing bracket after your class or function definition ends. But note that these missing bracket, is not the only reason for this type of error.

Erfankam
  • 380
  • 5
  • 15
0

class, interface, or enum expected

The above error is even possible when import statement is miss spelled. A proper statement is "import com.company.HelloWorld;"

If by mistake while code writing/editing it is miss written like "t com.company.HelloWorld;"

compiler will show "class, interface, or enum expected"

Prakhyat
  • 989
  • 8
  • 17
0

My App.java first line was package my.package.path;. package is a system keyword in java and cannot be used in the package path.

(I was using an example from stackoverflow with: mvn archetype:generate .. -DgroupId=my.package.path)

Curtis Yallop
  • 6,696
  • 3
  • 46
  • 36
-2

the main method should be declared in the your class like this :

public class derivativeQuiz_source{
    // bunch of methods .....

    public static void main(String args[])
    {
        // code 
    }
}
Community
  • 1
  • 1