//My objective of the program is using scanner class to get the input from the user and use a dialog box(JOptionPane) for output. When I compile the file I was able to enter the input from the scanner class, but I did not see the dialog box for the output using JOptionpane?

PLEASE HELP!!!

import javax.swing.*;
import java.util.Scanner;
import static java.lang.Math.*;

public class Volume
{

    public static void main (String[] args)
    {
        //Declaring Variables
        double radius;
        double height;
        //public static double length;
        double sphere;
        double cylinder = 0; 

        //Initialize Scanner to read from user
        Scanner input = new Scanner(System.in);

        //Read the radius from the user

        System.out.print("Enter a radius in meters :  ");
        radius = input.nextDouble();

        //Compute the volume of sphere

        sphere = 4/3*PI*Math.pow(radius, 3);

        //Compute the height of a cylinder

        height = cylinder/(PI*radius*radius);



        //Display their averages in the Dialog Box
        JOptionPane.showMessageDialog(null, "The volume of the sphere is: " + sphere + 
        "\n The height of a cylinder: " + height, "Calculations",JOptionPane.INFORMATION_MESSAGE);


    }


}

Recommended Answers

All 9 Replies

I have to use radius in meters?

My friend i think you cannot display output using JOptionPane method while you use scanner class.using scanner class(console input/output) you should use system.out.print().system is a class and print is a method.further if you use JOption method for your input then you can use either system.out.print() or dialog box.if i am wrong you can correct me.

here is a link which can help you http://leepoint.net/notes-java/io/io-console/console-dialog-comparison.html

The JOptionPane's showMessageDialog is indeed shown, but it is covered by the DOS window. After you move the DOS window away you will be able to see the JOpationPane's showMessageDialog. This is my observation.

You have the cylinder value set to 0. How does it calculate the height of the cylinder without the volume?

You have to add System.exit(0); at the end of your code, otherwise javac won't work properly.

Also you could improve your code by creating two object class somthing like:

public class Cylinder {

    private double radius;
    private double height;

    public Cylinder(double initialRadius, double initialHeight) {

    radius = initialRadius;
    height = initialHeight;

    }

    public Cylinder() {

    this(0,0);

    }

    public double getRadius() {

    return radius;

    }

    public double getHeight() {

    return height;

    }

    //Any other methods

}

You have to add System.exit(0); at the end of your code, otherwise javac won't work properly.

Sorry, but that is completely wrong. System.exit is always optional, and is mostly too dangerous to use in production systems.

You need to add a JFrame.

public class Volume
{

    public static void main (String[] args)
    {
        JFrame frame = new JFrame();

        /**
        *
        * rest of the code
        *
        */

        //Display their averages in the Dialog Box

        //add dialog box to frame
        JOptionPane.showMessageDialog(frame, "The volume of the sphere is: " + sphere + 
        "\n The height of a cylinder: " + height, "Calculations",JOptionPane.INFORMATION_MESSAGE);


    }


}

Hope that helps :)

Also the height calculation will always be 0. Is that you really wanted the height to be?

You need to add a JFrame.

Sorry, but that is completely wrong. You do not need a JFrame to use JOptionPane.showMessageDialog. Just use null as the first parameter.

also, this thread is 3 years old. if he still didn't found it, I'm pretty sure he isn't looking anymore.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.