Search code examples
javaswingcompiler-errorsjoptionpane

What causes error "error: '.class' expected"?


Please help me solve these errors.
These are the errors I'm getting after I have executed the java codes below.

/Deposit.java:15: error: '.class' expected
    double results=presentValue(double f, double r ,int n);
                                       ^
/Deposit.java:15: error: ';' expected
    double results=presentValue(double f, double r ,int n);
                                        ^
/Deposit.java:15: error: <identifier> expected
    double results=presentValue(double f, double r ,int n);
                                                    ^
/Deposit.java:15: error: ';' expected
    double results=presentValue(double f, double r ,int n);
                                                         ^
4 errors

import javax.swing.JOptionPane;

/*This program computes a customers present
deposit to make to obtain a desired future
value
*/
public class Deposit
{
  //Main method
  public static void main(String[] args)
  {

    //Calling the present value method
    double results=presentValue(double f, double r ,int n);

    //Displaying the present value 
    JOptionPane.showMessageDialog(null, "You have to deposit: $" +results);
  }

  public static double presentValue(double f, double r, int n)
  {
    //Declaring the input variable
    String input;

    //Taking inputs from the customer
    //Future value
    input = JOptionPane.showInputDialog("Enter your desired future value:");
    f = Double.parseDouble(input);

    //Annual Interest Rate
    input = JOptionPane.showInputDialog("Enter the annual interest rate:");
    r = Double.parseDouble(input);

    //Number of years
    input = JOptionPane.showInputDialog("Enter the number of years:");
    n = Integer.parseInt(input);

    //Calculating the present value the customer has to deposit
    double p = f/Math.pow((1+r), n);

    //Returning the value to the present value method
    return p;

    System.exit(0);
  }
}

Solution

  • Go with function without arguments, since yours doesn't do anything.

    public class Deposit
    {
      //Main method
      public static void main(String[] args)
      {
        double results=presentValue();
        ... // same code
      }
    
      public static double presentValue()
      {
        double f,r;
        int n;
        ... // same code
      }
    }