473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

JOptionPane error...

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

miles=JOptionPane.showInputDialog("Enter the number of miles driven : ");
gallons=JOptionPane.showInputDialog("Enter gallons of gas used : ");

miles1=Double.parseDouble(miles);
gallons1=Double.parseDouble(gallons);

MPG = miles1 / gallons1;

JOptionPane.showMessageDialog(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.333333333334 how do i restrict the result to 2 decimal places...
Sep 20 '07 #1
1 2099
r035198x
13,262 8TB
import javax.swing.JOptionPane;
public class MilesPerGallon
{
public static void main(String[] args)
{
String miles;
String gallons;
double MPG;
double miles1, gallons1;

miles=JOptionPane.showInputDialog("Enter the number of miles driven : ");
gallons=JOptionPane.showInputDialog("Enter gallons of gas used : ");

miles1=Double.parseDouble(miles);
gallons1=Double.parseDouble(gallons);

MPG = miles1 / gallons1;

JOptionPane.showMessageDialog(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.333333333334 how do i restrict the result to 2 decimal places...
1.) Use code tags when posting code
2.) Compare System.out.printf with the NumberFormat and take your pick.
Sep 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: IHateSuperman | last post by:
import javax.swing.JOptionPane; public class AddStaff{ public static void main(String args){ String StaffName; String StaffCurrPosition; String Salary; String SalaryInDouble; int x;
2
by: jek339 | last post by:
Hey, I'm totally new to Java, and I'm taking a class. My current assignment involves inputting 2 names and corresponding bdays for each name using the JOptionPane.showInputDialog etc. Anyway, I'm...
4
by: cnixuser | last post by:
Hello, I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with...
1
by: powerej | last post by:
program is suppose to ask for an input from 1-8, if the user enters a number outside of this range than it will continue to ask until it does, something is wrong and i cant seem to figure it out, i...
2
by: chokcheese | last post by:
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic);" line (it's in bold). I...
3
by: gator6688 | last post by:
import java.text.DecimalFormat; import javax.swing.JOptionPane; public class PayrollSystemTest { public static void main( String args ) { String workerType; String first;...
1
by: rleathem | last post by:
import java.io.*; import javax.swing.JOptionPane; public class Errors { public static void main(String args) { String line; int startMiles = 0;
6
by: safra | last post by:
hi everybody.. i need help with my assignment.i want to display a error message saying that the user has inputed a string where an integer is needed.because when i enter "h" (for an example) in the...
0
by: Mark Daniel | last post by:
For some reason, my final JOptionPane.showInputDialog is throwing an error when I try to compile, and I get some errors from the ActionPerformed area with an e.getSource. it is a swing array to take...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.