Results 1 to 8 of 8

Thread: Area problem

  1. #1
    rfore1 is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Unhappy 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

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    use code tags
    And I belive the problem is that you assign the value of area before getting the new "actual" radius.
    Change this:
    Java Code:
    double area=PI*(radius*radius);
    String str1;
    try{System.out.println("Please Enter Radius: ");
    str1=console.readLine();
    radius=Integer.parseInt(str1);
    to this:
    Java 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..

  3. #3
    rfore1 is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Unhappy 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

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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
    Java Code:
    double area=PI*(radius*radius);
    Erase the double, because u already said area is a double variable.

    Now it should work :D

  5. #5
    rfore1 is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default 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!!

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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;

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  8. #8
    rfore1 is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default Perfect!!

    It works!! THANKS ALOT...ADDEZ! You are a champ!

Similar Threads

  1. text area size problem
    By collin389 in forum AWT / Swing
    Replies: 7
    Last Post: 11-09-2009, 02:15 AM
  2. Area Add
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM
  3. [SOLVED] Text Area new line problem
    By Jay-1.1 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 04:18 AM
  4. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •