Results 1 to 8 of 8
  1. #1
    Deepthimadhu is offline Member
    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0

    Default While loop result to be displayed in a single dialog box using joption pane

    Hi I am new to java.

    I am computing loan amount inside while loop.I want the result of the while loop to be displayed in a single dialog box.
    like


    Here is my code:

    public class ComputeLoanUsingDialog {
    public static void main(String[] args)
    {
    double monthlyPayment ;
    double totalPayment;
    String loanString = JOptionPane.showInputDialog("Enter the LoanAmount: ");
    double loanAmt = Double.parseDouble(loanString);
    String periodString = JOptionPane.showInputDialog("Please enter the Loan Period : ");
    double years = Double.parseDouble(periodString);

    double i = 5.0;
    String output = "Interest Rate \t MonthlyPayment \t TotalPayment";
    JOptionPane.showMessageDialog(null, output);

    while(i<=8.0)
    {
    double monthlyInterestRate = i / 1200;

    monthlyPayment = loanAmt * monthlyInterestRate /
    (1-1 / Math.pow(1 + monthlyInterestRate, years * 12));
    totalPayment = monthlyPayment * 12 * years;

    monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
    totalPayment = (int) (totalPayment * 100) / 100.0;
    //Display results
    // String output = "Interest Rate \t MonthlyPayment \t TotalPayment";
    //JOptionPane.showMessageDialog(null, output);
    String result = "\t"+i+" \t"+monthlyPayment+ " \t " +totalPayment;
    JOptionPane.showMessageDialog(null, result);
    i = i + 1.0 / 8;

    }


    //String op = result;

    // JOptionPane.showMessageDialog(null,"Lab" + op);

    }
    }
    Attached Thumbnails Attached Thumbnails While loop result to be displayed in a single dialog box using joption pane-list.jpg  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    What have you tried? One way would be to build a String with everything you want to see and pass it to the showMessageDialog method.

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    I think the op does that already.

    I am not sure what the question is in this post.

  4. #4
    Deepthimadhu is offline Member
    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    Quote Originally Posted by benji2505 View Post
    I think the op does that already.

    I am not sure what the question is in this post.
    Hi benji

    op is showing only single row output.it is displaying only last row values.but i want all the rows to be displayed.can u help me how i can store the values in an array and display outside the loop .

    Thank you

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    Norm already gave you the answer: build a String in your loop and display it at the end. Here's something to get you going. Note that the "\n" is a new line.
    Java Code:
    String blah = "";
    while( somethingIsRunning ) {
       blah += something + " = " + somevalue + "\n";
    }
    JOptionPane.showMessage(myJFrame, blah);
    Or, if you want to go for the bonus points, use a StringBuilder. This has the advantage that the JVM does not need to create lots of new Strings, but I am pretty sure the compiler will convert your code to StringBuilder anyway... But just in case, here it is:
    Java Code:
    StringBuilder blah = "";
    while( somethingIsRunning ) {
       blah.append(something).append(" = ").append(somevalue).append("\n");
    }
    JOptionPane.showMessage(myJFrame, blah.toString());
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    Quote Originally Posted by SurfMan View Post
    ... but I am pretty sure the compiler will convert your code to StringBuilder anyway...
    The above code will result in a StringBuilder being created each time it goes around the loop.
    So yes, it will convert it into a StringBuilder, but only covering that one line, not the loop as a whole.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    Quote Originally Posted by Tolls View Post
    The above code will result in a StringBuilder being created each time it goes around the loop.
    So yes, it will convert it into a StringBuilder, but only covering that one line, not the loop as a whole.
    So I was right after all, in a way... :D But as this point I wouldn't worry about StringBuilders and performance. Just get it working.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    Deepthimadhu is offline Member
    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0

    Default Re: While loop result to be displayed in a single dialog box using joption pane

    Thanks a lot..got the output..

Similar Threads

  1. JOption Pane Warning message help
    By adjit in forum AWT / Swing
    Replies: 17
    Last Post: 12-09-2012, 07:52 PM
  2. Help with Int and JOption Input dialog
    By stephanie904 in forum New To Java
    Replies: 1
    Last Post: 02-29-2012, 01:22 AM
  3. Replies: 28
    Last Post: 12-30-2010, 11:40 PM
  4. Replies: 4
    Last Post: 03-20-2010, 04:17 AM
  5. Replies: 0
    Last Post: 12-06-2007, 06:10 PM

Posting Permissions

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