# Area problem

Printable View

• 12-10-2009, 09:36 PM
rfore1
Area problem
I am sure most of you have encountered this program. It is probably pretty easy for most of you.

Problem: I have gotten it to ask me to "Please Enter Radius". I enter a Radius of 5, for example, and it gives me an Area of 0.0. In the program I declared radius as "int radius=0" No matter what number I put in when it prompts me to enter radius, it will only compute using "0". How do I get it to give me the area based on what I enter at the prompt? Here is my program:

import java.io.*;

public class Project_2 {

static final double PI=3.141592653589793;

public static void main(String[] args) {

// TODO, add your application code
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
int radius=5;
double area=PI*(radius*radius);
String str1;
try{System.out.println("Please Enter Radius: ");
str1=console.readLine();
radius=Integer.parseInt(str1);

//Ignore the next 5 lines for now.
}catch(IOException e)
{System.out.println("Input error");
}catch(NumberFormatException e) {System.out.println(e.getMessage()+ "is not numeric");

area=PI*(radius*radius); //Enter your formula for the area here
}System.out.println("The Area is: " + (area));

} //Ends main
} //Ends Proj_2 Class
• 12-10-2009, 10:13 PM
Addez
use code tags
And I belive the problem is that you assign the value of area before getting the new "actual" radius.
Change this:
Code:

```double area=PI*(radius*radius); String str1; try{System.out.println("Please Enter Radius: "); str1=console.readLine(); radius=Integer.parseInt(str1);```
to this:
Code:

```String str1; try{System.out.println("Please Enter Radius: "); str1=console.readLine(); radius=Integer.parseInt(str1); double area=PI*(radius*radius);```
And it should all work..
• 12-10-2009, 10:31 PM
rfore1
Help!
Thanks for the reply!

:( After the fix I ran it and it gave me these errors:

"Cannot find symbol variable area" on this line:
area=PI*(radius*radius); //Enter your formula for the area here

"Cannot find symbol variable area" and "illegal start of type" on this line:
}System.out.println("The Area is: " + (area));

Here is the program I ran
-----------------------------------------------------------------

import java.io.*;

public class Project_2 {

static final double PI=3.141592653589793;

public static void main(String[] args) {

// TODO, add your application code
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
int radius=0;
String str1;
try{System.out.println("Please Enter Radius: ");
str1=console.readLine();
radius=Integer.parseInt(str1);
double area=PI*(radius*radius);

//Ignore the next 5 lines for now.
}catch(IOException e)
{System.out.println("Input error");
}catch(NumberFormatException e) {System.out.println(e.getMessage()+ "is not numeric");

area=PI*(radius*radius); //Enter your formula for the area here
}System.out.println("The Area is: " + (area));

} //Ends main
} //Ends Proj_2 Class
• 12-10-2009, 10:35 PM
Addez
Oh yea, it because the area variable is decleared within the try clause, which meens, outside the that clause it cannot find area, As u can see, at the bottom u try to print area but ur out of the try clause.

So do this, before the try write:
double area;
This will make sure the area is avalible outside the try clause.
Then in the try clause, at this line
Code:

`double area=PI*(radius*radius);`
Erase the double, because u already said area is a double variable.

Now it should work :D
• 12-10-2009, 10:47 PM
rfore1
This is frustrating me!
This thing has been kicking my tail the last couple of days.

OK....not as many errors but still getting this:

"variable area might not have been initialized" pointed to this line:

}System.out.println("The Area is: " + (area));

Thanks for your help!!
• 12-10-2009, 11:02 PM
Addez
yea, it's kinda of a warning, hate that one cause my ide refuse to compile if it gets that warning.
But u can erase it by adding = 0.0; at the double area; line.
So the new double area; is:
double area = 0.0;
• 12-10-2009, 11:11 PM
pbrockway2
Quote:

yea, it's kinda of a warning, hate that one cause my ide refuse to compile if it gets that warning.
But u can erase it by adding = 0.0; at the double area; line.

I think in Java it's a compiler error: ie not valid Java code.

The "may not have been initialized" error is very useful and you shouldn't try and defeat it. Declare variables close to where you use them, and initialise them when (if) you have valid data for them.

Probably in this case the "The Area is:..." message should go inside the try block.
• 12-10-2009, 11:14 PM
rfore1
Perfect!!
It works!! THANKS ALOT...ADDEZ! You are a champ!