Help with code, Says number is NaN and i cant format it
The code below is for figuring out a loan from user input. I seem to be having trouble with the code towards the bottom. I can not get my output statesment to be in currency format and it says some of the outputs are NaN? please help i have highlighted the area i think the problem is in.
Code :
import java.util.*;
import java.io.*;
import java.text.*;
public class Item1 {
public static void main(String[] args) throws NumberFormatException {
String loanS;
char loan;
String creditS;
char credit;
double moneyBorrow;
double timeYears;
double serviceCharge= 0;
double loanRate=0;
boolean goodData=true;
Scanner scan = new Scanner (System.in);
DecimalFormat decF = new DecimalFormat ("#,###,###.##");
System.out.println("What type of loan do you want:"
+"\n\t (F)irst Home Loan"
+"\n\t(H)omeImprovementLoan"
+"\n\t (C)ar Loan"
+"\n\t (P)ersonal Loan"
+"\n\t (B)usiness Loan");
System.out.println ("Enter your choice");
loanS = scan.next();
loan= loanS.charAt(0);
System.out.println ("choice is " + loan);
switch(loan){
case 'F':
case 'f':
loanRate=4.75/100/12;
break;
case 'H':
case 'h':
loanRate=5.25/100/12;
break;
case 'C':
case 'c':
loanRate=8.25/100/12;
break;
case 'P':
case 'p':
loanRate=9.5/100/12;
break;
case 'B':
case 'b':
loanRate=6/100/12;
default:
goodData=false;
}
if(goodData=false){
System.out.println("Incorrect Data");
}
System.out.println("What is your credit status"
+"\n\t(P)oor"
+"\n\t(F)air"
+"\n\t(G)ood"
+"\n\t(E)xcellent");
creditS = scan.next();
credit= creditS.charAt(0);
System.out.println("Answer: "+ credit);
System.out.println("How many thousands of dollars would you like to borrow?");
moneyBorrow = scan.nextDouble();
System.out.println("How many years will it take to pay off loan?");
timeYears= scan.nextDouble()*1000;
double moneyBorrowed =moneyBorrow *1000;
System.out.println(moneyBorrowed);
switch(credit)
{
case'P':
case'p':
serviceCharge=.03*moneyBorrowed;
break;
case'F':
case'f':
serviceCharge=.02*moneyBorrowed;
break;
case'G':
case'g':
serviceCharge=.01*moneyBorrowed;
break;
case'E':
case'e':
}
// Monthly payment = (mIR * LoanAmount) / (1 - (1 / (1+mIR)^(12*TimeInYears) ) )
//mIR = monthly interest rate = annual interest rate /12
//TimeInYears = number of years needed to pay off loan
[COLOR="Blue"]moneyBorrowed=moneyBorrowed+serviceCharge;
int mIR=0;
double monthlyPay =(mIR*moneyBorrowed)/(1-(1/Math.pow((1+mIR),(12*timeYears))));
double loanTotal=monthlyPay*timeYears*12;
double interestPaid=loanTotal-moneyBorrowed;
double interestPercent=interestPaid/loanTotal*100;[/COLOR]
NumberFormat currF = NumberFormat.getCurrencyInstance();
NumberFormat percentF = NumberFormat.getPercentInstance();
System.out.println(percentF.format(mIR));
System.out.println(currF.format(monthlyPay));
System.out.printf("interestRate: %11.2f%n",loanRate*100*12);
System.out.printf("Loan Amount: %11.2f%n",moneyBorrowed);
System.out.printf("Monthly Payment: %11.2f%n",monthlyPay);
System.out.printf("Total Amount Paid:%11.2f%n",loanTotal);
System.out.printf("Total Interest Paid: %11.2f%n",interestPaid);
System.out.printf("Interest Paid as a percent of loan: %11.2f%n",interestPercent);
// int timeInYears =
[/color]
Re: Help with code, Says number is NaN and i cant format it
Code :
double monthlyPay =(mIR*moneyBorrowed)/(1-(1/Math.pow((1+mIR),(12*timeYears))));
If mIR is zero, which you define as being zero, the equation boils down to :
(mIR*moneyBorrowed)/(1-(1/Math.pow((1+0),(12*timeYears))) =
(mIR*moneyBorrowed)/(1-(1/1)) =
(mIR*moneyBorrowed)/0 = division by zero = infinity = NaN
Division by zero is something that needs to be checked in complex code. In your case you should make sure mIR is never zero
Re: Help with code, Says number is NaN and i cant format it
thanks i took ur advice but now i cant format the numbers, i get a throw formatexception thats says f != java.lang.string any ideas?
Code :
import java.util.*;
import java.io.*;
import java.text.*;
public class Item1trytwo {
public static void main(String[] args) throws NumberFormatException {
char loan;
char credit;
double moneyBorrow;
double timeYears;
double serviceCharge= 0;
double loanRate=0;
boolean goodData=true;
double monthlyPay;
double loanTotal;
double interestPaid;
double interestPercent;
double moneyBorrowed;
String sMoneyBorrowed;
Scanner scan = new Scanner (System.in);
DecimalFormat decF = new DecimalFormat ("#,###,###.##");
System.out.println("What type of loan do you want:"
+"\n\t (F)irst Home Loan"
+"\n\t (H)ome Improvement Loan"
+"\n\t (C)ar Loan"
+"\n\t (P)ersonal Loan"
+"\n\t (B)usiness Loan");
System.out.println ("Enter your choice");
loan = scan.next().charAt(0);
System.out.println ("choice is " + loan);
switch(loan){
case 'F':
case 'f':
loanRate=.475/10/12;
break;
case 'H':
case 'h':
loanRate=.525/10/12;
break;
case 'C':
case 'c':
loanRate=.825/10/12;
break;
case 'P':
case 'p':
loanRate=.95/10/12;
break;
case 'B':
case 'b':
loanRate=.6/10/12;
default:
goodData=false;
}
if(goodData=false){
System.out.println("Incorrect Data");
}
System.out.println("What is your credit status"
+"\n\t(P)oor"
+"\n\t(F)air"
+"\n\t(G)ood"
+"\n\t(E)xcellent");
credit = scan.next().charAt(0);
System.out.println("Answer: "+ credit);
System.out.println("How many thousands of dollars would you like to borrow?");
moneyBorrow = scan.nextDouble();
System.out.println("How many years will it take to pay off loan?");
timeYears= scan.nextDouble()*1000;
moneyBorrowed =moneyBorrow *1000;
System.out.println(moneyBorrowed);
switch(credit)
{
case'P':
case'p':
serviceCharge=.03*moneyBorrowed;
break;
case'F':
case'f':
serviceCharge=.02*moneyBorrowed;
break;
case'G':
case'g':
serviceCharge=.01*moneyBorrowed;
break;
case'E':
case'e':
}
// Monthly payment = (mIR * LoanAmount) / (1 - (1 / (1+mIR)^(12*TimeInYears) ) )
//mIR = monthly interest rate = annual interest rate /12
//TimeInYears = number of years needed to pay off loan
moneyBorrowed=moneyBorrowed+serviceCharge;
double mIR;
mIR = loanRate/12;
monthlyPay =(mIR*moneyBorrowed)/(1-(1/Math.pow((1+mIR),(12*timeYears))));
loanTotal=monthlyPay*timeYears*12;
interestPaid=loanTotal-moneyBorrowed;
interestPercent=interestPaid/loanTotal*100;
NumberFormat currF = NumberFormat.getCurrencyInstance();
NumberFormat percentF = NumberFormat.getPercentInstance();
System.out.printf("interestRate: %11.2f \n",loanRate*100*12);
System.out.printf("Loan Amount: %11.2f\n",moneyBorrowed);
System.out.printf("Monthly Payment: %11.2f \n",monthlyPay);
System.out.printf("Total Amount Paid:%11.2f\n",loanTotal);
System.out.printf("Total Interest Paid: %11.2f\n",interestPaid);
System.out.printf("Interest Paid as a percent of loan: %11.2f\n",interestPercent);
// int timeInYears =
}
}
Re: Help with code, Says number is NaN and i cant format it
What do you mean by a 'formatException'? Is this a compilation error or runtime error? What line is there error on? As a side note, placing your code between two 'code' tags really help us be able to read your code and diagnose the problems.