# Can someone help me out on this?

• 10-11-2012, 12:37 AM
Denis2k11
Can someone help me out on this?
Trying to create a java program that will allow me to enter the Radius of a circle in cmd console window and will calculate the area of the circle but I KEEp getting error messages!!:@: Can someone tell me where I'm going wrong??

import java.util.Scanner;
public class Circle
{
public static void main(String[]args)
{
double Radius, pi = 3.14;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the Radius of the Circle: ");
Double Result;
Result = (Radius * Radius) * (pi);
System.out.print("The total area is: " + Result);
}
}
• 10-11-2012, 12:57 AM
milleniamisc
Re: Can someone help me out on this?
Here you go, try this one.

Code:

```import java.util.Scanner; public class Circle {         public static void main(String[]args) { double pi = 3.14; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the Radius of the Circle: "); double radius = Integer.parseInt(keyboard.nextLine()); double result = (radius * radius) * (pi); System.out.print("The total area is: " + result); } }```
There are a few things you overlooked. The data that you were inputting in the console was never stored in any variable to be called back. That is what keyboard.nextLine() is for. Now, this returns a value of type String, so in order to convert to int/double as it is needed in your code, that is the purpose of Integer.parseInt().

double radius = Integer.parseInt(keyboard.nextLine()); <- this takes your input, converts it to type int, and stores it in a variable called radius.

From this point, the next variable, result, just holds the answer which is shown in the finals System.out.print
• 10-11-2012, 01:03 AM
Daryn
Re: Can someone help me out on this?
Its the line Radius = Double();
I assume you were going for keyboard.nextInt(); wich will take the next number entered in the line.
Also I would suggest that next time you enter code use the correct format of before you code put[ CODE] and after [/ CODE] just with no spaces

p.s. also to square a number you can use Math.pow(yourbase,yourExponente)
• 10-11-2012, 01:06 AM
Denis2k11
Re: Can someone help me out on this?
Thanks so so much!!! Big help! :D However, you used Integer.parseInt, but this is for integers and I was going for doubles yet it STILL stores it as a double? How does it do that and not store it as an integer that you declared it as?
• 10-11-2012, 01:22 AM
milleniamisc
Re: Can someone help me out on this?
double, long, int, etc all store the same type of data, a numerical value. The only difference between each of those variable declarations is the range in which the stored data must be in. I don't remember the exact ranges off the top of my head though
• 10-11-2012, 02:17 AM
Jakeob
Re: Can someone help me out on this?
I thought you had to do "Double.parseInt". Also, I recommend using "Math.PI" in place of your pi double. ;)
• 10-11-2012, 02:29 AM
Fubarable
Re: Can someone help me out on this?
Quote:

Originally Posted by milleniamisc
Here you go, try this one.

Code:

```import java.util.Scanner; public class Circle {         public static void main(String[]args) { double pi = 3.14; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the Radius of the Circle: "); double radius = Integer.parseInt(keyboard.nextLine()); double result = (radius * radius) * (pi); System.out.print("The total area is: " + result); } }```
There are a few things you overlooked. The data that you were inputting in the console was never stored in any variable to be called back. That is what keyboard.nextLine() is for. Now, this returns a value of type String, so in order to convert to int/double as it is needed in your code, that is the purpose of Integer.parseInt().

double radius = Integer.parseInt(keyboard.nextLine()); <- this takes your input, converts it to type int, and stores it in a variable called radius.

From this point, the next variable, result, just holds the answer which is shown in the finals System.out.print

You should be using

Code:

`double radius = Double.parseDouble(someString);`
Not

Code:

`double radius = Integer.parseInt(someString);`
Your way you artificially restrict the user to entering only ints when this way the user can enter true double values such as 1.5 as input.

Rather than giving whole solutions, we much prefer to get the original poster's errors and try to have him work with *his* code towards a solution. This takes more work on your part, but in the end is usually much more satisfying for you, and a much better learning experience for the original poster.