PeterClark
PeterClark

Reputation: 1

What am I doing wrong in a string to double conversion?

I just bought a book on Java coding after taking a class a few years ago. I decided to start writing a program to get back in the swing of things. Anyways, my program to calculate the volume of a cylinder is having issues. Could you please critique it and tell me what's going on? If it's any help, I'm using JCreator.

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        string input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r;
        r=double.parsedouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h;
        h=double.parsedouble(input);
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

Upvotes: 0

Views: 195

Answers (5)

Darshan Lila
Darshan Lila

Reputation: 5868

I think your code should look like follwing: import javax.swing.*;

public class CandidateCode
{
    public static void main (String[] args)
    {
        String input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r=Double.parseDouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h=Double.parseDouble(input);;
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

There are couple of changes that I've made to it.

  1. Its String and not string.
  2. To parse double you should use Double class as double is primitive type.

Upvotes: 0

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9900

2 errors

1) it's String not string 2) double.parsedouble not correct it should be Double.parseDouble don't forget java is case sensitive and method names have camel case

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        String input;//first error you have types string //s should be capital
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        double r;
        r=Double.parseDouble(input);//2nd problem
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        double h;
        h=Double.parseDouble(input);
        double pi=3.1415926535;
        double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

Upvotes: 3

Kunjan Thadani
Kunjan Thadani

Reputation: 1670

import javax.swing.*;
public class Cylinder_Volume
{
    public static void main (String[] args)
    {
        String input;
        input=JOptionPane.showInputDialog("What is the radius of the cylinder?");
        Double r;
        r=Double.parseDouble(input);
        input=JOptionPane.showInputDialog("What is the height of the cylinder?");
        Double h;
        h=Double.parseDouble(input);
        Double pi=3.1415926535;
        Double volume=pi*r*r*h;
        System.out.println("The volume of the cylinder is: "+volume+".");
    }
}

Upvotes: 0

Matt Clark
Matt Clark

Reputation: 28639

Try

Double.parseDouble(0.0);

Use Double as an object, not a primitive. The call on the object, returns the primitive double.

Upvotes: 0

nitishagar
nitishagar

Reputation: 9413

you might want to use Double.parseDouble(input) instead of double.parsedouble(input).

Upvotes: 0

Related Questions