Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with code, Says number is NaN and i cant format it

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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.
    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]
    Last edited by helloworld922; March 3rd, 2010 at 02:39 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with code, Says number is NaN and i cant format it

    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

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    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 =	
     
     
     
     
        }
     
    }
    Last edited by helloworld922; March 4th, 2010 at 10:42 PM. Reason: please use [code] tags

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 03:08 PM
  2. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 10:30 AM
  3. Sting format give more truble
    By ahmethbaai in forum Java Theory & Questions
    Replies: 1
    Last Post: December 17th, 2009, 12:19 PM
  4. help with text format codes
    By vanchan09 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 03:12 PM
  5. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM

Tags for this Thread