Results 1 to 10 of 10
  1. #1
    skoutzombie is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Having trouble simplifying my answer

    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.

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

    Please give me any advice you have!
    Last edited by pbrockway2; 10-24-2011 at 03:42 AM. Reason: code tags added

  2. #2
    skoutzombie is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default Re: Having trouble simplifying my answer

    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.

    Java 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.
    Last edited by pbrockway2; 10-24-2011 at 03:46 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

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

  5. #5
    skoutzombie is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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:
    Java 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?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default 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

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

    Java 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:

    Java Code:
    String msg = String.format(
            "$%.2fc is the amount that you will need to start the account with", 
            presentAmount);
    JOptionPane.showMessageDialog(msg);

  7. #7
    skoutzombie is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

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

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default Re: Having trouble simplifying my answer

    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.

  9. #9
    skoutzombie is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble simplifying my answer

    I finally got it figured out. Thank you so much for your help and expertise pbrockway2.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

Similar Threads

  1. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 09:51 PM
  2. This will have an obvious answer!
    By MarkusHendersonicus in forum New To Java
    Replies: 6
    Last Post: 12-24-2010, 01:49 AM
  3. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-17-2010, 12:14 AM
  4. Replies: 0
    Last Post: 07-23-2009, 07:50 PM
  5. Replies: 3
    Last Post: 11-02-2008, 02:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •