Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Whats Wrong With My Code? Area Of a Circle (JAVA)

What I'm I missing here?

I don't get it. The Goal is to simply get the Area of a Circle that the User inputs.

I got the user input [*]

It just won't calculate the Area. It keeps giving me PI for anything I put it. I need it to Calculate the Area with the Given Radius from a user.

I'm pretty sure I'm missing something so simple What is it?

Code:

public class CircleArea {

/**
@param args the command line arguments
*/
public static void main(String[] args) {
// declare required variables

COPY THE CODE FROM HERE:

double radius, area ;
Scanner kbd = new Scanner (System.in);

// Get and show circle data
radius = 10.0;
area = 0.0;
area = Math.PI
radius * radius;
// TODO: should be pi times radius squared.
//Use constant variable PI to store 3.14159

System.out.println();
System.out.println("Enter the circle radius: ");
radius = kbd.nextDouble(); //User
System.out.println("Circle Measurements");
System.out.println(" Radius: " + radius + " cm");
System.out.println(" Area: " + area + " cm squared");

}

}

Yes I have to use the Math.PI Formula Given in Java.
Hey, try this. radius = Double.valueOf(kbd.nextLine());

Edit: My bad, you are doing the calculation for the area before asking user for the radius. So in a nutshell the area is always now area = Math.Pi 10 10.

How to fix this? Well, you have to ask for the userinput before doing any calculations so, where you now set the value of "radius = 10.0" replace that with
"radius = Double.valueOf(kbd.nextLine());" And it should work.
[Image: 1kIeAJohTqeXrSrWXtGGxA.png]

To this:

[Image: g2zRfpQaRiKYQpLvlXJEIg.png]
(Jan 28, 2019 02:33 AM)deluxe Wrote:  Hey, try this. radius = Double.valueOf(kbd.nextLine());

Edit: My bad, you are doing the calculation for the area before asking user for the radius. So in a nutshell the area is always now area = Math.Pi 10 10.

How to fix this? Well, you have to ask for the userinput before doing any calculations so, where you now set the value of "radius = 10.0" replace that with
"radius = Double.valueOf(kbd.nextLine());" And it should work.
[Image: 1kIeAJohTqeXrSrWXtGGxA.png]

To this:

[Image: g2zRfpQaRiKYQpLvlXJEIg.png]

Alright, So that Worked,but another issue I'm having now is that it's asking for the User input on the Radius TWICE NOW. I can simply just add in another line of code asking the user to Input the radius once more and boom problem solved BUT I need it to calculate it one time the user puts it in.

Code so far WITH THE ADDED LINE:


double radius, area ;
Scanner kbd = new Scanner (System.in);


System.out.println("Enter the circle radius: ");
radius = Double.valueOf( kbd.nextDouble()); <------- I've tried moving this around..... but the Code line with the "Area" seems to need it before the line for it to work.
area = Math.PI radius radius;
System.out.println("Can You Enter The Radius Again:");
radius = kbd.nextDouble(); //User
System.out.println("Circle Measurements");
System.out.println(" Radius: " + radius + " cm");
System.out.println(" Area: " + area + " cm squared");
(Jan 28, 2019 03:48 AM)CupOfJava Wrote:  Alright, So that Worked,but another issue I'm having now is that it's asking for the User input on the Radius TWICE NOW. I can simply just add in another line of code asking the user to Input the radius once more and boom problem solved BUT I need it to calculate it one time the user puts it in.

Code so far WITH THE ADDED LINE:
       

      double radius, area ;
Scanner kbd = new Scanner (System.in);


System.out.println("Enter the circle radius: ");
radius = Double.valueOf( kbd.nextDouble()); <------- I've tried moving this around..... but the Code line with the "Area" seems to need it before the line  for it to work.
area = Math.PI radius radius;
System.out.println("Circle Measurements");
System.out.println(" Radius: " + radius + " cm");
System.out.println(" Area: " + area + " cm squared");


You can just remove that second kbd call. So this one should work the way you like:



Quote:double radius, area ;
Scanner kbd = new Scanner (System.in);


System.out.println("Enter the circle radius: ");
radius = Double.valueOf( kbd.nextDouble());
area = Math.PI radius radius;
System.out.println("Circle Measurements");
System.out.println(" Radius: " + radius + " cm");
System.out.println(" Area: " + area + " cm squared");
(Jan 28, 2019 05:04 AM)deluxe Wrote:  You can just remove that second kbd call. So this one should work the way you like:

Awesome Fixed. I got 2 more projects like this due in 5hrs. Wish me Luck 🤝
Got them done. Got 2 more project's due in a few days I can breathe a bit.
If you need any help, feel free to hit me up! 🤝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SIM-3466371Jan 5, 2019 12:55 PM 3 163 Jan 5, 2019 12:55 PM
Last Post: HFDone
[C] by Hackerman_2018Nov 13, 2018 10:52 AM 7 163 Nov 13, 2018 10:52 AM
Last Post: HFDone
[.NET] by Mr. Money™Sep 21, 2017 07:01 AM 8 242 Sep 21, 2017 07:01 AM
Last Post: Dianab0lical.