// JP opened flex table

Click to See Complete Forum and Search --> : Need help casting a String into a double


Diesel_Power
May 12th, 2008, 02:34 PM
In my program I'm using the "Input Dialog" to have the user enter the radius of their circle. How do I change the input from a string into a double?
This is what I have so far,


import javax.swing.*;
public class circle
{
public static void main(String[] args)
{
double radius = double.parseDouble(JOptionPane.showInputDialog(null,"Enter your circle's radius.", "Radius", +
JOptionPane.INFORMATION_MESSAGE));

getCircumference(radius);

}

public static void getCircumference(String radius)
{
System.out.println("The circumference of the circle is: " + (2*3.14*radius));
}
}
[/quote]

and here is my error,


---------- Compile Java Source ----------
circle.java:11: class expected
double radius = double.parsedouble(JOptionPane.showInputDialog(null,"Enter your circle's radius.", "Radius", +
^
1 error

Output completed (0 sec consumed) - Normal Termination
[/quote]

Why does it want me to make a class? I don't completely understand the error.

Deliverance
May 12th, 2008, 03:13 PM
there is a primitive type 'double' and there is an Object type 'Double'. notice the capitals. the primitive type has no methods associated to it.

dlorde
May 12th, 2008, 06:47 PM
You will also have a problem with the getCircumference method which expects a String parameter for radius - but you have a double (or Double). It also tries to do the calculation with the String parameter, which obviously won't work.

If you make up your mind what type you are going to use for radius in your program, it will be a lot easier. In general, use the type most appropriate to the problem and what you will be doing in your code. If necessary convert from the input type to the internal type as soon as possible, and convert back to a displayable type (e.g. String) when you want to display the results. For example, a radius is a length measurement and will be used in calculations, so it makes sense to keep it internally as a numeric type, converting from the input type when it is input and to a suitable output type when it is output.

We are what we repeatedly do. Excellence, then, is not an act, but a habit...
Aristotle

Diesel_Power
May 12th, 2008, 09:39 PM
I'm trying to cast String radius into a double.

Sahil.kabra
May 13th, 2008, 03:23 AM
Use the method Double.parseDouble(String);

This returns you a primitive value.

Diesel_Power
May 13th, 2008, 05:08 PM
I'm using the Double.parseDouble(String) method


import javax.swing.*;
public class circle
{
public static void main(String[] args)
{
double radius = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter your circle's radius.", "Radius", +
JOptionPane.INFORMATION_MESSAGE));

getCircumference(radius);
}

public static void getCircumference(String radius)
{
System.out.println("The circumference of the circle is: " + (2*3.14*radius));
}
}


and now I'm getting this...


---------- Compile Java Source ----------
circle.java:14: getCircumference(java.lang.String) in circle cannot be applied to (double)
getCircumference(radius);
^
circle.java:20: operator * cannot be applied to double,java.lang.String
System.out.println("The circumference of the circle is: " + (2*3.14*radius));
^
2 errors

Output completed (4 sec consumed) - Normal Termination

keang
May 13th, 2008, 06:04 PM
Read the compiler messages - they tell you exactly what is wrong with your code.

circle.java:14: getCircumference(java.lang.String) in circle cannot be applied to (double)
getCircumference(radius);
Means: on line 14 of the cirlce class you are calling the method getCircumference(java.lang.String) but passing a double as the parameter when the method is declared as taking a String.
circle.java:20: operator * cannot be applied to double,java.lang.String
System.out.println("The circumference of the circle is: " + (2*3.14*radius));Means: on line 20 of the cirlce class you are trying to multiply a double with a String which is illegal

dlorde
May 13th, 2008, 06:11 PM
and now I'm getting this...I did tell you that you would get exactly these problems, and why you'd get them, in my last post. If you didn't understand it, you should ask. If you did understand it you could have avoided the problems. If you didn't read it, why bother asking for help in the first place?

Why do we never have time to do it right, but always have time to do it over?
Anon.

Diesel_Power
May 13th, 2008, 06:35 PM
I did tell you that you would get exactly these problems, and why you'd get them, in my last post. If you didn't understand it, you should ask. If you did understand it you could have avoided the problems. If you didn't read it, why bother asking for help in the first place?

Why do we never have time to do it right, but always have time to do it over?
Anon.

Alright I should of been more clear about this.

dlorde I DO understand that the second method will not work until I can cast the String "radius" in the main method into a double.

I DO know that I can't use a String in a mathematical equation, which is why I'm trying to cast the String "radius" into a double.

I'm assuming that once I can caste String "radius" into a double in the main method, that the second method will work.

My problem is that I can't seem to get the String "radius" to cast into a double.

Please review my code as I just figured out the [code][/code} ability. Hopefully it'll be easier to understand.

Martin O
May 13th, 2008, 07:31 PM
Alright I should of been more clear about this.

dlorde I DO understand that the second method will not work until I can cast the String "radius" in the main method into a double.

I DO know that I can't use a String in a mathematical equation, which is why I'm trying to cast the String "radius" into a double.

I'm assuming that once I can caste String "radius" into a double in the main method, that the second method will work.

My problem is that I can't seem to get the String "radius" to cast into a double.

Please review my code as I just figured out the [code][/code} ability. Hopefully it'll be easier to understand.

Ok so what part of post #7 did you not understand? :rolleyes:

dlorde
May 13th, 2008, 07:42 PM
My problem is that I can't seem to get the String "radius" to cast into a double..But you have already cast it to a double - that's what those last error messages (and Keang) were telling you - you were trying to pass that double to the getCircumference method, which was expecting a String.

I recommend that you take a couple of hours away from the problem and think about something else, or sleep on it. When you return, you'll see it a lot more clearly, trust me.

Every now and then go away, have a little relaxation, for when you come back to your work your judgment will be surer. Go some distance away because then the work appears smaller and more of it can be taken in at a glance and a lack of harmony and proportion is more readily seen...
Leonardo da Vinci

Diesel_Power
May 13th, 2008, 07:43 PM
Ok so what part of post #7 did you not understand? :rolleyes:

Bah! I just got it.

public static void getCircumference(Double radius)

dlorde
May 13th, 2008, 07:50 PM
public static void getCircumference(Double radius)Yes, but be aware that Double is a class wrapper for the double primitive type. Java 5 and above can automatically convert from Double objects to double and vice-versa, but if you're passing a double, your method should expect a double argument, not a Double (unless you have a good reason for doing it differently).

There is nothing so useless as doing efficiently that which should not be done at all...
P. Drucker

//JP added flex table