-4

I keep getting the same error which makes no sense to me.

Exercise03_01.java:5: error: class Exercise_03_01 is public, should be declared in a file named Exercise_03_01.java
public class Exercise_03_01 {

Heres my code:

import java.util.Scanner;

public class Exercise_03_01 {
    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);

    
        System.out.print("Enter a, b and c: ");
        // Prompt for user to enter the inputs
        
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();

        // Compute the discriminant of the equation.
        
        double discriminant = Math.pow(b, 2) - 4 * a * c;

        System.out.print("The equation has ");
        if (discriminant > 0)
        {
            double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);  
            double root2 = (-b - Math.pow(discriminant, 2)) / (2 * a);  
            System.out.println("two roots " + root1 + " and " + root2);
        }
        else if (discriminant == 0)
        {
            double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);
            System.out.println("one root " + root1);
        }
        else
            System.out.println("no real roots");
        }
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 3
    Well, is the code in a file called Exercise_03_01.java ? – OldProgrammer Sep 21 '20 at 17:34
  • 4
    The error message is telling you exactly what the problem is. Your filename needs to match the name of the public class. `Exercise03_01 != Exercise_03_01` – khelwood Sep 21 '20 at 17:34
  • 3
    Does this answer your question? [Can I compile a java file with a different name than the class?](https://stackoverflow.com/questions/1841847/can-i-compile-a-java-file-with-a-different-name-than-the-class) – qwerty Sep 21 '20 at 17:35
  • 1
    Either what they said or the place where you are executing from is wrong. Go to the file location in the command prompt/terminal and then run it(use `cd file_directory` to go there) – TheLegend42 Sep 21 '20 at 17:36
  • 1
    @TheLegend42 I think the error message indicates what the problem is in this case. – Dave Newton Sep 21 '20 at 17:39

2 Answers2

1

Your class does not share the same name with the filename. when a public class is declared the file and classname must be the same (exluding the .java)

RIVERMAN2010
  • 427
  • 3
  • 9
1

The error gave you the answer. as your file name is different.

class Exercise_03_01 is public, should be declared in a file named Exercise_03_01.java

File name and name of the Public class must be the same.

Please change the Filename to Exercise_03_01.java, It should fix this error.