Question

I'm trying to make a basic command-line accounting application in Java and this is what I have come up with so far:

import java.util.Scanner;

public class Accounting {
public static void main(String[] args) {


    while(true){
      Scanner input = new Scanner(System.in);
        String userinput = input.nextLine();

        String[] parts = userinput.split(" ");
        String part1 = parts[0];
        String part2 = parts[1];
        String part3 = parts[2];

        int a = Integer.parseInt(part1);
        float r = Float.parseFloat(part2);
        int t = Integer.parseInt(part3);
        int Total = (int) Math.pow(a + ( 1 + ( r / 100 )), t);
        System.out.println(Total);

You put in 3 inputs amount, rate, and time. I was told the formula is Total = a(1+(r/100))^t. Whenever i put an input like 25000 6.9 15 i get a number over 2 billion (2147483647) Any idea what I'm doing wrong?

Was it helpful?

Solution

Try it like this:

double total = a*Math.pow(1.0+(r/100.0), t);

OTHER TIPS

Math.pow(a + ( 1 + ( r / 100 )), t) is not the same as "Total = a(1+(r/100))^t". The correct (second) equation raises a value between 1 and 2 to t; the incorrect first one raises (in the case of your example) 25000 to 15.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top