0
votes

For some reason while running this code, I am not getting an output. Which I do not understand why. I'm sure it is basic reason, but everything seems correct to me.

package com.example.java;
import java.util.Scanner;

public class Main {

    public static void main (String[] args) throws Exception {
        String s1 = getInput("Enter value 1:");
        String s2 = getInput("Enter value 2:");

        double result = addValues(s1, s2);
        System.out.println("The answer is: " + result);


    }

    static String getInput(String prompt) {

        System.out.print(prompt);

        Scanner sc = new Scanner(System.in);

        return sc.nextLine();


    }

    static double addValues(String s1, String s2) {


        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 + d2;
        return result;


    }
}

And here is the output:

"C:\Program Files\Java\jdk1.8.0_73\bin\java" Exception in thread "main" java.lang.ClassNotFoundException:com.example.java.Main at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

Process finished with exit code 1

1
Did you compile the code? - Some guy
Have you compiled the code? Show us the command and output of the compilation step and the command you use to launch the code. - JP Moresmau
What is the name of your .java file? - DeveloperDemetri
Code has been compiled. Nothing It's just under Main.java - JD1738
Then it's most likely an issue with the package. See @liminal's answer. - DeveloperDemetri

1 Answers

1
votes

If you have already compiled the class, the issue may be a Classpath issue. Make sure the directory where package com.example.java (or current directory, denoted with .) is in your Classpath