---------------------------------------------------------------------------------------------------------------------
I'm having problems with a static method. I'm trying to return a double after formatting a double with NumberFormatter; which returns a String. I'm trying to use the parseDouble method in the Double class but I get an error. My code is:
import java.text.NumberFormat;
public class Loop2
{
public static void main(String[] args)
{
showTable(1000, 0.12, 12);
System.out.println("\n*********************\n");
showTable(5000, 0.10, 60);
}
public static void showTable(double borrowed, double yearlyRate, int months)
{
System.out.println("Ammount Borrowed " + borrowed);
System.out.println("Yearly Intrest Rate " + yearlyRate);
System.out.println("Number of Months " + months + "\n");
System.out.println("Total Due is " + amountDue(borrowed, (yearlyRate / 12), months));
}
public static double amountDue(double principal, double monthlyRate, int period) //Error here
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
double amountDue = Double.parseDouble(formatter.format(principal * Math.pow(1 + monthlyRate, period)));
return amountDue;
}
}
And my error is :
Exception in thread "main" java.lang.NumberFormatException: For input string: "$1,126.83" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Loop2.amountDue(Loop2.java:31) at Loop2.showTable(Loop2.java:21) at Loop2.main(Loop2.java:7)


Sign In
Create Account


Back to top









