Results 1 to 8 of 8
  1. #1
    len
    len is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  4. #4
    len
    len is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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).

  6. #6
    len
    len is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    does not want to compile correctly...
    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)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by len View Post
    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
    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

  1. Volume of Intersection
    By afifi in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 10:42 AM
  2. Creating a cylinder object
    By b_erten in forum New To Java
    Replies: 8
    Last Post: 07-25-2010, 08:43 PM
  3. java app - volume of cylinder
    By tomwaits4noman in forum Java Applets
    Replies: 1
    Last Post: 02-27-2010, 05:12 PM
  4. Morph Cone to Cylinder!
    By aRTx in forum Advanced Java
    Replies: 2
    Last Post: 05-09-2009, 05:50 PM
  5. Cylinder
    By Manikyr in forum New To Java
    Replies: 1
    Last Post: 02-28-2009, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •