0

I have been trying to work around this for the last few hours and decided I should ask for some advice. I am writing a code that calculates the square footage of a running-track-shaped sign. it then calculates the price based on square footage (), and then prompts for any text and charges per character. It runs smoothly through the loop without apparent issue, but once it reaches the end there is an if/else sequence to search for a sentinel value. The problem is, my code isn't seeming to read that if/else. it prompts for the user to enter "quit" if they are done, but instead of collecting user input it jumps right back to prompting for details to calculate square footage. how do i get my Scanner to accept the users input and affect the loop?

    import java.util.Scanner;

public class DonutSign {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
            //initiate all variables
        boolean ans = false;
        double r = 0;
        double sideA = 0;
        double sideB = 0;
        double angle = 0;
        String text = "INTIAL";
        
        double circleArea = 0;
        double parArea = 0;
        double finalArea = 0;
        double sizePrice = 0;
        double textPrice = 0;
        double finalPrice = 0;
        String response = "quit";
    
            //do-while price calculation
        do{
                //have all of the variables entered
            System.out.println("Enter the radius of the circles:");
            r = input.nextDouble();
            System.out.println("Enter the first side of the parallelogram:");
            sideA = input.nextDouble();
            System.out.println("Enter the second side of the parallelogram:");
            sideB = input.nextDouble();
            System.out.println("Enter the angle of the parallelogram:");
            angle = input.nextDouble();
            System.out.println("Enter the string you would like on your sign:");
            text = input.next();
            
                //calculate area of the circles
            circleArea = Math.PI * Math.pow(r, 2);
                //calculate area of parallelogram
            parArea = sideA * sideB * (Math.sin(Math.toRadians(angle)));
                //add surface area values together and calculate price
            finalArea = circleArea + parArea;
            sizePrice = finalArea * 2.95;
                //calculate price of text and add values
            textPrice = ((text.length() + 1)    * 1.45);
            finalPrice = textPrice + sizePrice;
            System.out.print("$");
            System.out.printf("%.2f", finalPrice);
            System.out.println();
            
            
                //If-else to exit the loop
            System.out.println("Would you like to create another sign? Enter quit to exit.");
            response = input.nextLine();
            if(response == "quit"){
                ans = true;
            }
            else{
                ans = false;
            }
        } while (ans != true);      
    }
}
Emiel Zuurbier
  • 11,936
  • 2
  • 9
  • 22
gravyGus
  • 1
  • 2

1 Answers1

0

There is a good answer here:

Java Scanner doesn't wait for user input

The problem is that nextInt() does not consume the '\n', so the next call to nextLine() consumes it and then it's waiting to read the input for y.

You need to consume the '\n' before calling nextLine().

Try adding an input.nextLine() after your input.next() or input.nextDouble()

drnugent
  • 1,393
  • 7
  • 18