-2

I wrote a rock, paper, scissors game that had a play again feature.

I can't understand what is missing there.

I just need it to ask the user if they want to calculate another circle.

Code snippet:

package circleapp;
import java.util.Scanner;

public class CircleApp {

   static Scanner sc = new Scanner(System.in);

   public static void main(String[] args) {
       String answer;
       String calcAgain;

       do{
           System.out.print("Enter a radius: ");
            double radius = sc.nextDouble();

            //Diameter=2 * radius
            double diameter= 2 * radius;
            System.out.println("Diameter: " + diameter);

            //Circumference= 2 * PI * radius
            double circumference= Math.PI * 2*radius;
            System.out.println( "Circumference: "+circumference);

            //Area= PI*radius*radius
            double area = Math.PI * (radius * radius);
            System.out.println("Area: " + area);


            //Would the user like to calculate again?

            System.out.print("Would you like to enter another circle? (Y/N)");
            calcAgain = sc.nextLine();

        }
        while (calcAgain.equalsIgnoreCase("Y"));
    }     
}

How to solve this problem?

catch23
  • 13,661
  • 38
  • 120
  • 194
  • 1
    `nextDouble()` doesn't consume the whole line. So `nextLine()` will consume it. Add an additional `nextLine()` statement before retrieving the client answer (y or n). It would give : `sc.nextLine(); calcAgain = sc.nextLine();` – davidxxx Oct 13 '18 at 13:35
  • You can go for `next()` instead of `nextLine()`. This will solve your issue. – Shahid Oct 13 '18 at 13:37
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Nicholas K Oct 13 '18 at 13:57

2 Answers2

0

Instead of reading full line with nextLine() use simple next():

import java.util.Scanner;

public class CircleApp {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        String calcAgain;

        do {
            System.out.print("Enter a radius: ");
            double radius = sc.nextDouble();

            //Diameter=2 * radius
            double diameter = 2 * radius;
            System.out.println("Diameter: " + diameter);

            //Circumference= 2 * PI * radius
            double circumference = Math.PI * 2 * radius;
            System.out.println("Circumference: " + circumference);

            //Area= PI*radius*radius
            double area = Math.PI * (radius * radius);
            System.out.println("Area: " + area);


            //Would the user like to calculate again?
            System.out.print("Would you like to enter another circle? (Y/N) ");
            calcAgain = sc.next();

        } while (calcAgain.equalsIgnoreCase("Y"));
    }
}

Output will be something like:

Enter a radius: 4
Diameter: 8.0
Circumference: 25.132741228718345
Area: 50.26548245743669
Would you like to enter another circle? (Y/N): y
Enter a radius: 5
Diameter: 10.0
Circumference: 31.41592653589793
Area: 78.53981633974483
Would you like to enter another circle? (Y/N): n

Also, my suggestion is to write your code much readable. For example, what from below will be better for reading:

double circumference = Math.PI*2*radius;

or

double circumference = Math.PI * 2 * radius;

BTW when asking a question here just specify your problem and omit another detail.

catch23
  • 13,661
  • 38
  • 120
  • 194
  • Yea i'm still really bad at formatting. Thanks, I appreciate it! – Kaitlin Oct 13 '18 at 14:59
  • @Kaitlin You can use an autoformatting shortcut for your IDE. BTW At SO we have an appreciation with accepting answer and upvoting. – catch23 Oct 13 '18 at 15:22
0

change

calcAgain = sc.nextLine();

to

calcAgain = sc.next();

This should work

Raymonoir
  • 3
  • 4