0

I am a beginner in Java and my professor wants us to create a program that involves some math. The thing is that he wants the calculations in a separate method. Can someone please help me. The program will not run the result for some reason. I tried looking around this website and could not find the answer. Here is the code below:

import javax.swing.JOptionPane;

public class forFun {
    public static void main(String[] args)
    {
        double x, y, z;
        String xVal, yVal, zVal;
        xVal = JOptionPane.showInputDialog("Enter first integer: ");
        yVal = JOptionPane.showInputDialog("Enter second integer: ");
        zVal = JOptionPane.showInputDialog("Enter third integer: ");

        x = Double.parseDouble(xVal);
        y = Double.parseDouble(yVal);
        z = Double.parseDouble(zVal);

        System.exit(0);
    }

    public static void sumOfStocks(double x, double y, double z)
    {
        double result = x * y * z;

        System.out.println("The product of the integers is: " + result);
        System.exit(0);
    }
}
4
  • You NEVER call the method. Commented Sep 3, 2016 at 2:08
  • 1
    Also the method shouldn't be void, shouldn't print out anything or exit. Instead, it should return the double result value, and let the main program print it out. Regardless, the solution to this is to read any tutorial on Java methods. Any. Commented Sep 3, 2016 at 2:09
  • 1
    Start with: Defining Methods, then Passing Information to a Method or a Constructor and finally Returning a Value from a Method. Commented Sep 3, 2016 at 2:11
  • As Hovercraft Full Of Eels said, you never called the method. You should add sumOfStocks(x, y, z); just before the System.exit(0); to make that method call. Commented Sep 3, 2016 at 2:14

1 Answer 1

1

Here is the example of how the code should be

import javax.swing.JOptionPane;

public class forFun {
public static void main(String[] args)
{
    double x, y, z;
    String xVal, yVal, zVal;
    xVal = JOptionPane.showInputDialog("Enter first integer: ");
    yVal = JOptionPane.showInputDialog("Enter second integer: ");
    zVal = JOptionPane.showInputDialog("Enter third integer: ");

    x = Double.parseDouble(xVal);
    y = Double.parseDouble(yVal);
    z = Double.parseDouble(zVal);
    double result = sumOfStocks(x, y, z);
    System.out.println("The result is %d", result);
    System.exit(0);
}

public static double sumOfStocks(double x, double y, double z)
{
    double result = x * y * z;
    return result;

}
}
Sign up to request clarification or add additional context in comments.

1 Comment

I made a mistake, instead of System.out.println, it should be System.out.printf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.