-1
import java.text.NumberFormat;

 public class Mortgage {
 public static void main(String[] args) {
    int p = 1000000;
   NumberFormat percent = NumberFormat.getPercentInstance();
   double r = Double.parseDouble(percent.format(3.92*12));

    int t = (int)(r);
    double n;
    n = Math.pow(30,12);
    int f = (int) Math.floor(n);

    int a =(1+t)^f;
    int b = (a-1);
    int c = (t*a)/b;
    int m = p*c;
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    String result = currency.format(m);

    System.out.println(result);

    }
 }

I have tried to changed r to int but I still got exception. What am I not writing correctly?

prosoitos
  • 4,676
  • 5
  • 19
  • 35
Amaka_k
  • 7
  • 4
  • 1
    What is the exception you got? Can you include that in your question? – Faraz Oct 27 '19 at 20:42
  • 1
    What exception ? What error ? don't force us to run the code to see it ;) – azro Oct 27 '19 at 20:42
  • What did you expect at the end ? change the name of your variable, it's not understandable – azro Oct 27 '19 at 20:44
  • Always, always, always include the full text of the error message if you're running into an error you can't rectify. You're asking strangers to help you for free -- don't make things harder than they need to be. In case anyone was wondering: `Exception in thread "main" java.lang.NumberFormatException: For input string: "4,704%"` – MarsAtomic Oct 27 '19 at 20:48
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "4,704%" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at com.example.tunji.Mortgage.main(Mortgage.java:9) – Amaka_k Oct 27 '19 at 20:54
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "4,704%" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at com.example.tunji.Mortgage.main(Mortgage.java:9) – Amaka_k Oct 27 '19 at 20:54
  • @Amaka_k if you want to add additional information to your question always edit the question instead of burying the information in the comments. This is because the comments may become hidden, if there are too much of them. – Jannik Oct 27 '19 at 21:02
  • okay thank you, its my second time. – Amaka_k Oct 27 '19 at 21:17
  • `int a =(1+t)^f;` does not do what you think it does. `^` is the binary XOR operator in Java, not exponentiation. To do exponentiation you need `double a = Math.pow(1 + t, f);` Also - what do you think `Math.pow(30,12)` does? 30 to the twelfth power is a humongous number, and you won't be able to hold that in an `int`; your call to `Math.floor(n)` is meaningless. – Erwin Bolwidt Oct 27 '19 at 21:43

3 Answers3

1

You use NumberFormat.getPercentInstance() to format your number. This adds a % symbol and other number formatting (depending on your default locale). Then the Double.parseDouble(...) call fails because the number is not a pure double number.

There is no need to format and parse the number, you can just assign it directly to the double variable as it is a constant anyways.

Jannik
  • 1,393
  • 1
  • 11
  • 18
1

I see several problems.

    double n;
    n = Math.pow(30,12);
    int f = (int) Math.floor(n);

30 to the 12th power. That does not make sense for a 30 year mortgage Did you mean 30*12 for 360 pay periods. Or possibly Math.pow(30,1+montlyRate) where monthlyRate = (AR/100)/12 and AR = annual rate).

    int a =(1+t)^f;

The operator ^ is not power but an exclusive OR. You probably didn't want that either.

I recommend you check out this Wiki entry on computing Mortage Payments

Here is one way to do calculate it and then display it per month.

      double in = 5.5; // annual percentage rate
      double mo_rate = (in / 100) / 12.; // monthly rate
      double PV = 400_000.; // present value (cost of the house).
      double f = Math.pow(1 + mo_rate, 360); // factor resulting from the linear
                                             // expansion

      double payment = mo_rate * PV * f / (f - 1); // Monthly payment including
                                                   // interest and
                                                   // principal

      System.out.printf("Monthly payment is %7.2f%n", payment);

Note: different banks and/or countries may do it differently.

WJS
  • 22,083
  • 3
  • 14
  • 32
  • i have seen your code but i have not started using iteration can you help me fix that without it? – Amaka_k Oct 28 '19 at 23:50
  • @Amaka_k The edited answer simply prints out the monthly payment. The iteration was just to print the amortization table and was not necessary. – WJS Oct 29 '19 at 01:35
0

The answer is in the exception java.lang.NumberFormatException: For input string: "4,704%"


  1. percent.format(3.92*12) returns the String : 4 704 % and this can't be parsed to double, because of the space and the % symbol, you just need to multiply it by 100 to consider it as a percentage

    double r = 3.92 * 12 * 100;
    
  2. As you're using only ints you fall into the int division problem and you'll get a 0 at the end, so you may use at leat one double ot cast one durint the division

    int a = (1 + t) ^ f;
    double b = (a - 1);
    double c = (t * a) / b;
    double m = p * c;
    // OR
    int a = (1 + t) ^ f;
    int b = (a - 1);
    double c = (t * a) / (double) b;
    double m = p * c;
    
  3. Also ^ is thr XOR symbol, to use power computation :

    double a = Math.pow(1+t, f);
    

For the result, it depends on r:

  • double r = 3.92 * 12 * 100; gives -10 308,38 €
  • double r = 3.92 * 12; gives 999 998,95 €

And use names that explain what the variable is, as much as possible

azro
  • 35,213
  • 7
  • 25
  • 55