473,608 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JOptionPane error...

1 New Member
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...
Sep 20 '07 #1
1 2121
r035198x
13,262 MVP
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.
Sep 21 '07 #2

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

Similar topics

2
1465
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
3044
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 confused by Sun's site and I keep getting error messages. Below is what I have and the error message when I try to compile. Any help would be much appreciated. Thanks! package javaapplication1; /** * * @author Jillian */ public class...
4
3629
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 a "JMenuBar" object, with "JMenu" objects, such as "File" with "JMenuItem" objects such as "Save". The Modifications that I am attempting to make to the program are designed to give actual functionality to the "JMenuItems", starting with the "Save"...
1
1951
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 am stuck. Also my if else to see if the user catches the wave seems right to me but only works like half the time. I just really want to know what i am doing wrong here. import javax.swing.*; // gets option pane import...
2
2629
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 know it has something to do with an object being null, but I just can't figure out which... Can anyone help? Any suggestions are greatly appreciated :) (By the way, I translated the code to english but I might have forgotten to translate a word, so if...
3
2571
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; String last;
1
1424
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
2190
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 x coordinate field the system crashes.i want to display an error message instead. /** * A program that simulates an ant taking a step on a sheet of paper. stage 2 * * @author (safra) * @version (18/8/2010) */
0
1623
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 entered numbers and average them while also having the option to view the highest and lowest number. I am very new to this, and I am not sure where I went wrong. here is the code. Thanks import java.awt.*; import java.awt.event.*; import...
0
8000
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.