We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,492 Members — Technology Publication meets Social Media

# Need help with Math.pow ...... Program won't work

import java.util.Scanner;
import java.util.*;

public class Interest {

public static void main(String[]args)
{
double
interest,
primary,
period,
amount,
yearly,
monthly,
daily,
weekly,
quaterly,
bianually,
time;

Scanner Scanner = new Scanner(System.in);

System.out.println(" How much money is the person wanting to loan?");
primary = Scanner.nextInt();

System.out.println("Please enter the rate of interest.");
interest = Scanner.nextInt();

System.out.println("Over how many years will they pay the loan back?");
time = Scanner.nextInt();

System.out.println("Do you wish to have this amount compounded daily, weekly,");
System.out.println("quaterly, bianually or yearly?");
period = Scanner.nextInt();

if
(period == daily)
amount = primary*Math.pow(1.0 + interest/365, time*365);

if
(period == weekly)
amount = primary*Math.pow(1.0 + interest/52, time*52);

if
(period == quaterly)
amount = primary*Math.pow(1.0 + interest/4, time*4);

if
(period == bianually)
amount = primary*Math.pow(1.0 + interest/2, time*2);

if
(period == yearly)
amount = primary*Math.pow(1.0 + interest, time);

System.out.println("Your final amount is R" + amount);

}
}

import java.util.Scanner;
import java.util.*;

public class Interest {

public static void main(String[]args)
{
double
interest,
primary,
period,
amount,
yearly,
monthly,
daily,
weekly,
quaterly,
bianually,
time;

Scanner Scanner = new Scanner(System.in);

System.out.println(" How much money is the person wanting to loan?");
primary = Scanner.nextInt();

System.out.println("Please enter the rate of interest.");
interest = Scanner.nextInt();

System.out.println("Over how many years will they pay the loan back?");
time = Scanner.nextInt();

System.out.println("Do you wish to have this amount compounded daily, weekly,");
System.out.println("quaterly, bianually or yearly?");
period = Scanner.nextInt();

if
(period == daily)
amount = primary*Math.pow(1.0 + interest/365, time*365);

if
(period == weekly)
amount = primary*Math.pow(1.0 + interest/52, time*52);

if
(period == quaterly)
amount = primary*Math.pow(1.0 + interest/4, time*4);

if
(period == bianually)
amount = primary*Math.pow(1.0 + interest/2, time*2);

if
(period == yearly)
amount = primary*Math.pow(1.0 + interest, time);

System.out.println("Your final amount is R" + amount);

}
}
4
Contributors
3
Replies
3 Weeks
Discussion Span
9 Months Ago
Last Updated
4
Views
psy.blast
Newbie Poster
4 posts since Jul 2012
Reputation Points: 0
Skill Endorsements: 0

"won't work" tells us nothing. Next time post the complete text of your error messages.
Anyway, your variables daily,weekly,quaterly,bianually,yearly are not initialised.

JamesCherrill
... trying to help
Moderator
8,660 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

There are a few things wrong with the program.

(Apart from the variables not being initiated)
(1)
line 34: period = Scanner.nextInt();

The scanner expects an integer value. It isn't clear whether you want input to be a String or an integer. If you need the user to enter "daily", then variable period should be a String. In which case line 34 should look like this:

period = Scanner.next();

(2)
Don't use "Scanner", rather change it to "console" or "sc" to avoid confusion:

line 21: Scanner console = new Scanner(System.in);

(3)
Your interest value is going to get entered as a whole number. This is normally a percent, so for example; 7 % should be 0.07 for the calculation.

See if you can find the other mistakes yourself and come back ;-)

Starstreak
Light Poster
48 posts since Aug 2012
Reputation Points: 13
Skill Endorsements: 0
1. Your variables are not initialized.

2. And even if you think you did, with "double", you used them like "Integers"

primary = Scanner.nextInt();

And also, you shoud try another name instead of Scanner to avoid confusion.

steph7
Newbie Poster
1 post since Aug 2012
Reputation Points: 0