JOptionPane error...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smarteric
    New Member
    • Sep 2007
    • 1

    #1

    JOptionPane error...

    import javax.swing.JOp tionPane;
    public class MilesPerGallon
    {
    public static void main(String[] args)
    {
    String miles;
    String gallons;
    double MPG;
    double miles1, gallons1;

    miles=JOptionPa ne.showInputDia log("Enter the number of miles driven : ");
    gallons=JOption Pane.showInputD ialog("Enter gallons of gas used : ");

    miles1=Double.p arseDouble(mile s);
    gallons1=Double .parseDouble(ga llons);

    MPG = miles1 / gallons1;

    JOptionPane.sho wMessageDialog( null,"Your Car's Mileage is : " + MPG + "\n");
    System.exit(0);
    }
    }

    this returns me a dialog where in I enter 400 then another dialog where in i enter 3.. the result is 133.33333333333 4 how do i restrict the result to 2 decimal places...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Smarteric
    import javax.swing.JOp tionPane;
    public class MilesPerGallon
    {
    public static void main(String[] args)
    {
    String miles;
    String gallons;
    double MPG;
    double miles1, gallons1;

    miles=JOptionPa ne.showInputDia log("Enter the number of miles driven : ");
    gallons=JOption Pane.showInputD ialog("Enter gallons of gas used : ");

    miles1=Double.p arseDouble(mile s);
    gallons1=Double .parseDouble(ga llons);

    MPG = miles1 / gallons1;

    JOptionPane.sho wMessageDialog( null,"Your Car's Mileage is : " + MPG + "\n");
    System.exit(0);
    }
    }

    this returns me a dialog where in I enter 400 then another dialog where in i enter 3.. the result is 133.33333333333 4 how do i restrict the result to 2 decimal places...
    1.) Use code tags when posting code
    2.) Compare System.out.prin tf with the NumberFormat and take your pick.

    Comment

    Working...