# Need to figure out what I did wrong with DecimalFormat coding.

Page 1 of 1

## 6 Replies - 8425 Views - Last Post: 26 January 2011 - 08:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=212059&amp;s=c2e08d6c1f521f45894b283749e0ec49&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 coyboss

Reputation: 0
• Posts: 64
• Joined: 28-January 09

# Need to figure out what I did wrong with DecimalFormat coding.

Posted 25 January 2011 - 07:04 PM

HI all,

I am trying to use DecimalFormat to ensure that my monetary values show up with 2 decimal places after the decimal.

I tried to use the "example" on http://download.orac...FormatDemo.java to help me figure out what I am doing, It was not very helpful.

Here is my code:
```import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Mortgage{
public class DecimalFormat //the class for decimal formatting

{
public static void main(String args[])
{
// declare and construct variables
int loanAmt = 200000; // principal loan amount
int[] loanTerm = {84, 180 ,360}; // loan term for 7 years, 15 years, and 30 years
int loanYears; // indicates the loan term in years
double[] intRate = {5.35, 5.50, 5.75}; // interest rates for 7 years, 15 years, and 30 years
double newIntRate; // displays interest rate calculation
double monthlyPay = 0; // displays monthly payment calculation
DecimalFormat money = new DecimalFormat("\$0.00"); // displays mortgage in decimal format
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
NumberFormat formatter = new DecimalFormat("#0.000");

// displays messages to indicate purpose of program and conditions in the console window
System.out.println();
System.out.println("Welcome to the Mortgage Payment Calculator");
System.out.println();
System.out.println("This program will calculate three separate mortgage payments for a \$" + loanAmt);
System.out.println("with the following loan terms and interest rates: (1) 7 years @ 5.35%");
System.out.println("\t\t\t\t\t\t  (2) 15 years @ 5.50%");
System.out.println("\t\t\t\t\t\t  (3) 30 years @ 5.75%");
System.out.println();
System.out.println("The results are as follows:");

int i;
for (i = 0; i <= 2; i++)
{

intRate[i] = (intRate[i] /12 * .01);
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));

// displays results for each loan in the console window
System.out.println();
System.out.println("The mortgage payment for a \$" + loanAmt + " loan for " + loanTerm[i] /12 + " years at");
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
System.out.println("The Decimal Value is:"+formatter.format(amount));

{

}
}
}
}
}

```

and here are the errors I am getting:

O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:18: non-static variable this cannot be referenced from a static context
DecimalFormat money = new DecimalFormat("\$0.00"); // displays mortgage in decimal format
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:19: non-static variable this cannot be referenced from a static context
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:20: non-static variable this cannot be referenced from a static context
NumberFormat formatter = new DecimalFormat("#0.000");
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:44: cannot find symbol
symbol : method format(double)
location: class Mortgage.DecimalFormat
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:44: cannot find symbol
symbol : method format(double)
location: class Mortgage.DecimalFormat
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:45: cannot find symbol
symbol : variable amount
location: class Mortgage.DecimalFormat
System.out.println("The Decimal Value is:"+formatter.format(amount));
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: inner classes cannot have static declarations
public static void main(String args[])
^
7 errors

Tool completed with exit code 1

HOw do I correct these issues and what did I do wrong please.

Thanks in advance for all the help.

IKE

Is This A Good Question/Topic? 1

## Replies To: Need to figure out what I did wrong with DecimalFormat coding.

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 11034
• Posts: 41,275
• Joined: 27-December 08

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 25 January 2011 - 09:36 PM

With 44 posts you still do not know how to properly indent your code? Seriously, help us help you.

Your problem is that you define DecimalFormat as a class (specifically an inner class) which overshadows java.text.DecimalFormat. You just need to import it to use it.

### #3 coyboss

Reputation: 0
• Posts: 64
• Joined: 28-January 09

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 26 January 2011 - 04:06 AM

Sorry Mac,

I wasn't thinking about indentation when I wrote it, my apologies.

OK I got rid of the DedimalFormat class, but now its giving other errors, which don't seem to make sense.

Here's the edited code:
```import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Mortgage{

{
public static void main(String args[])
{
// declare and construct variables
int loanAmt = 200000; // principal loan amount
int[] loanTerm = {84, 180 ,360}; // loan term for 7 years, 15 years, and 30 years
int loanYears; // indicates the loan term in years
double[] intRate = {5.35, 5.50, 5.75}; // interest rates for 7 years, 15 years, and 30 years
double newIntRate; // displays interest rate calculation
double monthlyPay = 0; // displays monthly payment calculation
DecimalFormat money = new DecimalFormat("\$0.00"); // displays mortgage in decimal format
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
NumberFormat formatter = new DecimalFormat("#0.000");

// displays messages to indicate purpose of program and conditions in the console window
System.out.println();
System.out.println("Welcome to the Mortgage Payment Calculator");
System.out.println();
System.out.println("This program will calculate three separate mortgage payments for a \$" + loanAmt);
System.out.println("with the following loan terms and interest rates: (1) 7 years @ 5.35%");
System.out.println("\t\t\t\t\t\t  (2) 15 years @ 5.50%");
System.out.println("\t\t\t\t\t\t  (3) 30 years @ 5.75%");
System.out.println();
System.out.println("The results are as follows:");

int i;
for (i = 0; i <= 2; i++)
{

intRate[i] = (intRate[i] /12 * .01);
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));

// displays results for each loan in the console window
System.out.println();
System.out.println("The mortgage payment for a \$" + loanAmt + " loan for " + loanTerm[i] /12 + " years at");
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
System.out.println("The Decimal Value is:"+formatter.format(amount));

{

}
}
}
}
}

```

and the errors:
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: ';' expected
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: ')' expected
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: illegal start of expression
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: ';' expected
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: illegal start of expression
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:9: ';' expected
public static void main(String args[])
^
O:\college stuff\PRG420 JAVA PROGRAMMING I\Ind Assign\Wk4\Mortgage.java:54: class, interface, or enum expected
}
^
9 errors

Tool completed with exit code 1

Thanks again for the help.

IKE

### #4 Dogstopper

Reputation: 2898
• Posts: 11,122
• Joined: 15-July 08

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 26 January 2011 - 04:09 AM

Perhaps one of your key problems is here:
```
public class Mortgage{
{

```

Notice the TWO "{"'s?

### #5 coyboss

Reputation: 0
• Posts: 64
• Joined: 28-January 09

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 26 January 2011 - 05:55 AM

Doh!!!!

Its the little things that get you in this! LOL

Thanks Dog.
I'll fix that later when I get home

And then post anybother issues I have, if any.

Ike

This post has been edited by coyboss: 26 January 2011 - 05:56 AM

### #6 coyboss

Reputation: 0
• Posts: 64
• Joined: 28-January 09

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 26 January 2011 - 05:55 PM

OK so now I have gotten rid of the error messages, but I still have a problem ( of sorts).

It prints the amounts for the monthly payments with 2 decimal places, but the loan amount should show \$200000.00, but it only shows \$200000. HOw can I fix this issue please.

Here is the updated code:

```import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Mortgage

{
public static void main(String args[])
{
// declare and construct variables
int loanAmt = 200000; // principal loan amount
int[] loanTerm = {84, 180 ,360}; // loan term for 7 years, 15 years, and 30 years
int loanYears; // indicates the loan term in years
double[] intRate = {5.35, 5.50, 5.75}; // interest rates for 7 years, 15 years, and 30 years
double newIntRate; // displays interest rate calculation
double monthlyPay = 0; // displays monthly payment calculation
DecimalFormat money = new DecimalFormat("\$0.00"); // displays mortgage in decimal format
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
NumberFormat formatter = new DecimalFormat("#0.000");

// displays messages to indicate purpose of program and conditions in the console window
System.out.println();
System.out.println("Welcome to the Mortgage Payment Calculator");
System.out.println();
System.out.println("This program will calculate three separate mortgage payments for a \$" + loanAmt);
System.out.println("with the following loan terms and interest rates: (1) 7 years @ 5.35%");
System.out.println("\t\t\t\t\t\t  (2) 15 years @ 5.50%");
System.out.println("\t\t\t\t\t\t  (3) 30 years @ 5.75%");
System.out.println();
System.out.println("The results are as follows:");

int i;
for (i = 0; i <= 2; i++)
{

intRate[i] = (intRate[i] /12 * .01);
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));

// displays results for each loan in the console window
System.out.println();
System.out.println("The mortgage payment for a \$" + loanAmt + " loan for " + loanTerm[i] /12 + " years at");
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));

{

}
}
}
}

```

I almost forgot, I also need to make sure this program is OOP, any assistance with that would be greatly appreciated.

Thanks again for all the help.

IKE

This post has been edited by coyboss: 26 January 2011 - 05:57 PM

### #7 coyboss

Reputation: 0
• Posts: 64
• Joined: 28-January 09

## Re: Need to figure out what I did wrong with DecimalFormat coding.

Posted 26 January 2011 - 08:17 PM

OK It appears I got this mostly resolved,
with the exception of the decimal places on the
mortgage amount.

I am going to close this quesition and I want to thank all those who helped me!!

IKE