im having a little troubele with this problem. i am working on an intrest calculator and have hit a snag.its saying there is error with my Math.pow punction can someone help please. ill add what i have so far below

import java.util.Scanner;

public class Calculator {

    public static void main (String[] args) {


        Scanner input = new Scanner(System.in);

        System.out.print("Input a Principal Amount:");  
        float P = input.nextInt();

        System.out.print("Input the number of terms (in years):");
        float t = input.nextFloat();
        t = t*12;

        System.out.print("Enter the number of times intrest is compounded per year:");
        float n = input.nextFloat();

        System.out.print("Input a Annual Intrest (in decimal form):" );
        float r = input.nextFloat();

        System.out.print("Enter the method to calculate the intrest 1 = Simple , 2 = Monthly Compounded 3 = Daily Compounded" );
        int method = input.nextInt();
        float A = 0;
        double pwr = 0;
        switch (method) {

            case 1: A = (P * r * t);

            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);

            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);

            default : System.out.print("An invalid option has been made");

        }

        System.out.print("total calculated intrest" + A);
        System.out.print("Total amount to be repaid" + (A+P));

    }

}

Recommended Answers

All 5 Replies

define "it is saying". what error message do you get?

What exactly is the error message?

Exact error message (when compiling from the command line):

C:\Users\mvmalderen\Desktop>javac Calculator.java
Calculator.java:31: error: ')' expected
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                       ^
Calculator.java:31: error: illegal start of expression
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                           ^
Calculator.java:31: error: ';' expected
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                               ^
Calculator.java:31: error: not a statement
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                ^
Calculator.java:31: error: ';' expected
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                 ^
Calculator.java:31: error: not a statement
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                  ^
Calculator.java:31: error: ';' expected
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                   ^
Calculator.java:31: error: not a statement
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                     ^
Calculator.java:31: error: ';' expected
            case 2: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                      ^
Calculator.java:33: error: ')' expected
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                       ^
Calculator.java:33: error: illegal start of expression
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                           ^
Calculator.java:33: error: ';' expected
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                               ^
Calculator.java:33: error: not a statement
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                ^
Calculator.java:33: error: ';' expected
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                 ^
Calculator.java:33: error: not a statement
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                  ^
Calculator.java:33: error: ';' expected
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                   ^
Calculator.java:33: error: not a statement
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                     ^
Calculator.java:33: error: ';' expected
            case 3: A = (P *(1 + (r/n))Math.pow(n,t)-P);
                                                      ^
18 errors

@Andrew:

You used an invalid syntax.
Also note that Math.pow returns double and not float.
Since you're assigning the result to a variable declared as of type float you'll need an explicit cast (or declare your 'A' variable as of type double).
Also note that you better don't choose uppercased names for variables unless they are constants.

As for the syntax error I'll use the following piece of your code as an example:
(P *(1 + (r/n))Math.pow(n,t)-P)

You got your formula wrong here. Math.pow(n, t) will raise n to the t-th power.
So putting Math.pow after (P *(1 + (r/n)) won't raise (P *(1 + (r/n)) to the n*t-th power.

Try the following piece of code: P * Math.pow(1 + r / n, n * t).

Thanks for the help i see what i was doing im still new to jave and the math functions confuesed me a little.

suggetion :

place 'break' statement at the end of each 'case' statement in switch() statement

otherwise all three cases will execute for each time when the control comes in the switch() statement

happy coding

commented: Good catch! ;) +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.