# Having trouble simplifying my answer

• 10-24-2011, 02:20 AM
skoutzombie
Hello all. I am new to Java and have a homework assignment that I have been reluctant to ask for help on. I am going to post the problem and then I will post what I have and explain what my issue is.

The problem wants us to figure out the following equation for various sets of data: P = F/(1+r)^n
This is about banking where P stands for your Present Value, F stands for your Future Value, r stands for your Rate of Interest and n stands for the number of years you leave the money in the account. Your goal is to find out how much money you need (P) by putting in how much money you want to have (F) at which rate of interest(r) for how long(n).

I am going to give one set of data that we have to input

Future Value(F) \$10,000
Annual Interest Rate (r)% 5.75
Number of Years (n) 10

Here is the code I used.

Code:

```import javax.swing.JOptionPane;//Importing the JOptionPane public class presentValue {       public static void main(String[] args)                 {                   String inputString; //Declaring string                            double presentAmount;//Declaring variables                         double futureAmount;                         double rateInterest;                         double numberOfYears;                         double onePlusRateInterest = 0;                                                                         {                                do//starting the loop                         {                       inputString = JOptionPane.showInputDialog("Enter the future value " + //entering the data for futureAmount                                 "that you will want in this account. To end, enter 0 in all boxes. ");                                 futureAmount = Double.parseDouble(inputString);                               inputString = JOptionPane.showInputDialog("Enter the annual " +//entering the data for rateInterest                     "interest rate.");                         rateInterest = Double.parseDouble(inputString);                                                                 inputString = JOptionPane.showInputDialog("Enter the number of years " +//entering the data for numberOfYears                                         "you plan to let the money sit in the account.");                                 numberOfYears = Double.parseDouble(inputString);                                                                                                                 onePlusRateInterest = (1 + rateInterest);                //setting up my equation                                 presentAmount = futureAmount/(java.lang.Math.pow(onePlusRateInterest,numberOfYears));//equation                                                                 JOptionPane.showMessageDialog(null, "The amount you need to deposit " + //showing amount needed to start account                   "today to reach desired amount is \$" + presentAmount);                                                                                                                                }                 while (futureAmount != 0); //while statement                 JOptionPane.showMessageDialog(null, "Thank you.");//ending message                 }                         System.exit(0);//end                 }   }```
I am having an issue with my results page where I get a number such as: 5.09286626092347E-5
I would really like to have it be something more readable and I haven't been able to find a solution yet so I am asking for some help.

• 10-24-2011, 02:34 AM
skoutzombie
Re: Having trouble simplifying my answer
I am going to go ahead and apologize. It was not formatted like this when I hit "post" There were indentations. I am sorry for whatever confusion this causes.
• 10-24-2011, 02:42 AM
pbrockway2
Re: Having trouble simplifying my answer
Quote:

I would really like to have it be something more readable
There is a String method that will format a numeric value into a string.

Code:

```public class Eg {         public static void main(String[] args) {                 double value = 3.14159;                     // 3 decimal places, padded with spaces so that it is 8 characters wide                 System.out.println(String.format("%8.3f", value));                         // A "money" format                 System.out.println(String.format("\$%.2fc", value));                 value = 5.09286626092347E-5;                 System.out.println(String.format("\$%.2fc", value));         } }```
The link given above has another which describes the format string syntax. ("%.2f" etc) But it's pretty straightforward: <width>.<precision>f. The "f" stands for floating point.

The last example perhaps needs explaining. E-5 means "times ten to the power -5" ie 5.09 millicents. If you get an answer like that you might want to check your formula. In particular check that how you are doing arithmetic with percentages is sensible.
• 10-24-2011, 02:44 AM
pbrockway2
Re: Having trouble simplifying my answer
You put [code] at the start of the code and [/code] at the end. That way the indents etc are formatted correctly by the forum software.
• 10-24-2011, 03:02 AM
skoutzombie
Re: Having trouble simplifying my answer
I figured out how to fix the percentage issue. Not sure where my head was there. I am just changing the way the information is input.

For the main issue I was having this is what I am putting in:
Code:

```JOptionPane.showMessageDialog(String.format("\$%.2fc", presentAmount + " is the amount" +                                         " that you will need to start the account with."));```
Am I not using it correctly with the JOptionPane?
• 10-24-2011, 03:09 AM
pbrockway2
Re: Having trouble simplifying my answer
There's a problem with the parentheses. String.format(etc) returns a string which you can add to the rest of the message

Code:

```JOptionPane.showMessageDialog(String.format("\$%.2fc", presentAmount) + " is the amount" +                     " that you will need to start the account with.");```
Notice how there is a parenthesis after the two arguments to format().

Or you can put all of the message into the format string. I think this is more readable.

Code:

```JOptionPane.showMessageDialog(String.format(         "\$%.2fc is the amount that you will need to start the account with",         presentAmount));```
A third variant - again in the interests of legibility:

Code:

```String msg = String.format(         "\$%.2fc is the amount that you will need to start the account with",         presentAmount); JOptionPane.showMessageDialog(msg);```
• 10-24-2011, 03:16 AM
skoutzombie
Re: Having trouble simplifying my answer
This is the error message I am getting with this. Do you know what it means? It seems that every time I get close, something else blocks my path.
Code:

```presentValue.java:33: error: no suitable method found for showMessageDialog(String)                                 JOptionPane.showMessageDialog(String.format("\$%.2fc", presentAmount) + " is the amount" +                                                 ^     method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is not applicable       (actual and formal argument lists differ in length)     method JOptionPane.showMessageDialog(Component,Object,String,int) is not applicable       (actual and formal argument lists differ in length)     method JOptionPane.showMessageDialog(Component,Object) is not applicable       (actual and formal argument lists differ in length) 1 error```
• 10-24-2011, 04:01 AM
pbrockway2
Re: Having trouble simplifying my answer
Quote:

error: no suitable method found for showMessageDialog(String)
The thing to do is go to the API documentation and looks at what it says about the showMessageDialog() method. It requires two arguments: the component within whose frame the dialog will be displayed, and the string (or other object) to be displayed as a message. Note what it says about that first argument.
• 10-24-2011, 04:16 AM
skoutzombie
Re: Having trouble simplifying my answer
I finally got it figured out. Thank you so much for your help and expertise pbrockway2.
• 10-24-2011, 04:39 AM
pbrockway2
Re: Having trouble simplifying my answer
You're welcome.