Results 1 to 8 of 8
- 01-23-2011, 05:07 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
computing cylinder volume using JOptionPane
hi
i just started java in community college and writing code is something that is is totally new to me.
i have been trying to write a program that computes volume of a cylinder and the problem i riun into is that it compiles ok but when i enter 2 numbers (legnth and radius) in my input pane the prgram crashes ,if i enter just one,i get erroneous results
here is the code
import javax.swing.JOptionPane;
public class Volume {
public static void main (String[] args) {
String gettempstring = JOptionPane.showInputDialog ("enter radius + legnth of cylinder") ;
double legnth = Double.parseDouble(gettempstring);
double radius = Double.parseDouble(gettempstring);
double area = (radius * radius * 3.14159);
double volume =(area * legnth);
String output = ("the area is " + area + "the volume is" + volume );
JOptionPane.showMessageDialog(null, output);
}
}
i appreciate all help and would just like to be pointed in the right direction
thank you
len
- 01-23-2011, 05:12 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Define "the prgram crashes". Does it throw an exception? If you enter 2 numbers (separated by a space) into the dialog, the parse function will throw a NumberFormatException (read the API on this function). You must either split the string on whitespace, or show 2 dialogs for each number
-
Second all that doWhile recommends. Also, if you are brave and want to get fancy, you could create a JPanel, add 2 JLabels and 2 JTextFields, and display this in a JOptionPane (use the Object parameter). Then after the JOptionPane returns you can extract the information from each JTextField. Good luck!
- 01-23-2011, 05:43 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
define program crashes
if i enenter 2 numbers in the first dialouge box Jgrasp returns this statement on thew bottom pane
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 2"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at Volume.main(Volume.java:6)
----jGRASP wedge2: exit code for process is 1.
-
doWhile explains why your code is causing problems. You have one String that has two numbers separated by a space, and you try to parse the same String twice. What number does "1 2" represent? It doesn't. Again as doWhile explains, you need to use the String to get two separate Strings that hold no spaces, which String#split(" ") can do for you (read the API on this).
- 01-23-2011, 05:53 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
tried to add another input
import javax.swing.JOptionPane;
public class Volume {
public static void main (String[] args) {
String gettempstring = JOptionPane.showInputDialog ("enter radius ") ;
String gettempstring = JOptionPane.showInputDialog ("enter legnth of cylinder") ;
double legnth = Double.parseDouble(gettempstring);
double radius = Double.parseDouble(gettempstring);
double area = (radius * radius * 3.14159);
double volume =(area * legnth);
String output = ("the area is " + area + "the volume is" + volume );
JOptionPane.showMessageDialog(null, output);
does not want to compile correctly...
i am too new (21/2 weeks to try to get fancy
thank you for your help
len
- 01-23-2011, 06:57 PM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Phrases like this mean very little without the compile time error that you receive...and the fact that the code above does not look complete there could be a number of reasons for the error (which we don't have any information about). Recommend you post the full error and the full code wrapped in the [code][/code] tags. This will help us address your compile error (there will be other errors in behavior after that but one thing at a time - I suggest you step through your code logically - think about it from a computer standpoint - to address these once it compiles)does not want to compile correctly...
-
If you have an error with compilation or any other error message, you will want to post that message here. I'm guessing that one of your problems is that you're declaring the gettempstring twice, and you can't do that. Why not declare it once, use it to get one number String, immediately parse that String and then get the second input and immediately parse the returned String.
Similar Threads
-
Volume of Intersection
By afifi in forum Advanced JavaReplies: 0Last Post: 01-07-2011, 10:42 AM -
Creating a cylinder object
By b_erten in forum New To JavaReplies: 8Last Post: 07-25-2010, 08:43 PM -
java app - volume of cylinder
By tomwaits4noman in forum Java AppletsReplies: 1Last Post: 02-27-2010, 05:12 PM -
Morph Cone to Cylinder!
By aRTx in forum Advanced JavaReplies: 2Last Post: 05-09-2009, 05:50 PM -
Cylinder
By Manikyr in forum New To JavaReplies: 1Last Post: 02-28-2009, 03:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks