Dialog Output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Synapse
    New Member
    • Dec 2006
    • 26

    Dialog Output

    Hello gurus!

    I need a lil help with my program. Its just a piece of code that will compute the 4 basic arithmetic function. but my problem is to display the answer in a single message box.

    here's my code below.

    import javax.swing.*;
    import java.io.*;

    public class arithmetic2 {
    public static void main( String args[] ) {

    String fnum, snum;
    float a, b, sum, diff, quot, prod;

    fnum = JOptionPane.sho wInputDialog( "Enter first integer" );
    snum = JOptionPane.sho wInputDialog( "Enter second integer" );
    a = Float.parseFloa t(fnum);
    b= Float.parseFloa t(snum);

    sum = a + b;
    diff = a - b;
    quot = a / b;
    prod = a * b;

    JOptionPane.sho wMessageDialog(
    null, "The sum is elvar" +sum, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The difference is " + diff, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The quotent is " + quot, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The product is " + prod, "Results",
    JOptionPane.PLA IN_MESSAGE );

    System.exit( 0 );
    }
    }


    I used to separate the reuslts in 4 message box. It must be only one.
    I need help badly guys.. Thank you.

    Oh by the way how do i separate the whole number and the decimal?
    like 1.23 it should be

    The whole number is: 1
    The decimal value is: .23

    I'll be waiting for your help and highly appreciate it. Thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Synapse
    Hello gurus!

    I need a lil help with my program. Its just a piece of code that will compute the 4 basic arithmetic function. but my problem is to display the answer in a single message box.

    here's my code below.

    import javax.swing.*;
    import java.io.*;

    public class arithmetic2 {
    public static void main( String args[] ) {

    String fnum, snum;
    float a, b, sum, diff, quot, prod;

    fnum = JOptionPane.sho wInputDialog( "Enter first integer" );
    snum = JOptionPane.sho wInputDialog( "Enter second integer" );
    a = Float.parseFloa t(fnum);
    b= Float.parseFloa t(snum);

    sum = a + b;
    diff = a - b;
    quot = a / b;
    prod = a * b;

    JOptionPane.sho wMessageDialog(
    null, "The sum is elvar" +sum, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The difference is " + diff, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The quotent is " + quot, "Results",
    JOptionPane.PLA IN_MESSAGE );

    JOptionPane.sho wMessageDialog(
    null, "The product is " + prod, "Results",
    JOptionPane.PLA IN_MESSAGE );

    System.exit( 0 );
    }
    }


    I used to separate the reuslts in 4 message box. It must be only one.
    I need help badly guys.. Thank you.

    Oh by the way how do i separate the whole number and the decimal?
    like 1.23 it should be

    The whole number is: 1
    The decimal value is: .23

    I'll be waiting for your help and highly appreciate it. Thank you
    1.) Please use code tags when posting code.
    2.) Don't you think it better to use a switch here? The one box for display problem would then be solved.
    3.) To seperate 1 and 23 in 1.23, have a look at the java.lang.Math class. It has methods for extracting the integer part from a decimal value.

    Comment

    Working...