Results 1 to 7 of 7
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Angry 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: ");
    Radius = Double();
    Double Result;
    Result = (Radius * Radius) * (pi);
    System.out.print("The total area is: " + Result);
    }
    }

  2. #2
    milleniamisc is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Can someone help me out on this?

    Here you go, try this one.

    Java 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

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    166
    Rep Power
    2

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

  4. #4
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

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

  5. #5
    milleniamisc is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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

  6. #6
    Jakeob is offline Java Apprentice
    Join Date
    Oct 2012
    Location
    On my computer
    Posts
    10
    Rep Power
    0

    Default 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. ;)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can someone help me out on this?

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

    Java 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

    Java Code:
    double radius = Double.parseDouble(someString);
    Not

    Java 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.
    Last edited by Fubarable; 10-11-2012 at 02:31 AM.

Tags for this Thread

Posting Permissions

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