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 2 of 2

Thread: Problems getting floating point number to show in GUI...

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problems getting floating point number to show in GUI...

    Hello! So I'm trying to get the very last part of this code to show up as "The total accumulation is: $(whateverMynumberEquals)" in the GUI. The problem is that I'm using a floating point and I have no clue how to put it in proper form so that it shows up correctly. I put what I THOUGHT it would be below, so you can tell me what's right and what's not. Or should I make it a variable and THEN put it in the GUI? I just need the box to show the number with two decimal points only. Anyways, as you can see, I don't know what I'm doing with it, so please help! Thanks!

    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class CompoundInterest {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner QWERTY = new Scanner(System.in);
    		JOptionPane.showMessageDialog(null, "Welcome to the Compound Interest Calculator!");
    		JOptionPane.showMessageDialog(null, "We are going to ask you for certain information in order calculate your interest!");
    		System.out.println();
     
    		double P = 0;
    		P = Double.parseDouble(
    				JOptionPane.showInputDialog("What is the initial deposit (principle)? Numbers only please!"));
    		JOptionPane.showMessageDialog(null, "The value entered is: $" + P);
     
    		double r = 0;
    		r = Double.parseDouble(
    			JOptionPane.showInputDialog("What is the annual interest rate in percent? [xx.yy]"));
    		JOptionPane.showMessageDialog(null, "The value entered is: " + r);
     
    		int q = 0;
    		q = Integer.parseInt(
    			JOptionPane.showInputDialog("What is the number of times the compounding is done per year?"));
    		JOptionPane.showMessageDialog(null, "The value entered is: " + q);
     
    		double n = 0;
    		n = Double.parseDouble(
    			JOptionPane.showInputDialog("How many years is the principle left in deposit?"));
    		JOptionPane.showMessageDialog(null, "The value entered is: " + n);
     
    		r = (r/100);
    		double exponent = Math.pow(1 + r/q, n*q);
     
    		double A = 0;
    		A = P * exponent;
    		JOptionPane.showMessageDialog(null, "The total accumulation is:" + 
    			System.out.printf("$%.2f\n", A));
     
     
    	}
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Problems getting floating point number to show in GUI...

    Don't use System.out.printf() as that method prints things to the console.

    Instead look in the String API docs for a method that formats things the same way printf() does, but which returns a String.

Similar Threads

  1. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 11:18 AM
  2. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  3. Declaring floating point number depending on an if statement
    By Keys767 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2011, 02:50 PM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 03:43 PM
  5. Quick Java Number pad problems
    By SkippyTHEpimp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 06:00 AM