Hack Forums
[Java] Whats Wrong With My Code? Area Of a Circle (JAVA) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Whats Wrong With My Code? Area Of a Circle (JAVA) (/showthread.php?tid=5934820)



Whats Wrong With My Code? Area Of a Circle (JAVA) - CupOfJava - Jan 28, 2019

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.


RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - deluxe - Jan 28, 2019

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]


RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - CupOfJava - Jan 28, 2019

(Jan 28, 2019 02:33 AM)ᴅᴇʟᴜxᴇ 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");


RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - deluxe - Jan 28, 2019

(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");




RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - CupOfJava - Jan 28, 2019

(Jan 28, 2019 05:04 AM)ᴅᴇʟᴜxᴇ 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 🤝


RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - CupOfJava - Jan 31, 2019

Got them done. Got 2 more project's due in a few days I can breathe a bit.


RE: Whats Wrong With My Code? Area Of a Circle (JAVA) - Khamul - Jan 31, 2019

If you need any help, feel free to hit me up! 🤝