JAVA curly braces

661 Views Asked by At

Receiving a syntax error on my curly braces but no matter how many I close it still gives an error. Can't figure it out. Thank you for any advice on this I am at a loss. I have tried adding more and removing more. I had the count of how many I needed to close but still getting an error.

import java.util.Scanner;

public class Paint1 {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        
        final double squareFeetPerGallons = 350.0;
        
        // Implement a do-while loop to ensure input is valid
        // Prompt user to input wall's height
        do {
            
        System.out.println("Enter wall height (feet): ");
        wallHeight = scnr.nextDouble();
            while (!scnr.hasNextInt()) {
                System.out.printf("\"%s\" is not a valid number.\n");
                System.out.println("Please enter wall height in feet: ");
            } while (wallHeight < 0) {
            
        
        // Implement a do-while loop to ensure input is valid
        // Prompt user to input wall's width
            do {
        System.out.println("Enter wall width (feet): ");
        wallWidth = scnr.nextDouble(); // changed wallHeight to wallWidth
        while (!scnr.hasNextDouble()) {
            System.out.printf("\"%s\" is not a valid number.\n");
            System.out.println("Please enter wall width in feet: ");
        } while (wallWidth < 0) {
        
        
            
        
        // Calculate and output wall area
        wallArea = wallHeight * wallWidth;
        System.out.println("Wall area: " + wallArea + " square feet"); // added variable

        // Calculate and output the amount of paint (in gallons) needed to paint the wall
        gallonsPaintNeeded = wallArea/squareFeetPerGallons;
        System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // changed variable to correct case
        
            }
}
        
3

There are 3 best solutions below

0
On

You seem to have an extra curly brace in the } while (wallWidth < 0) { line. Have you tried removing that? The opening brace is fine, but the one at the end of the line might be giving you issues.

0
On

you tried to implement a do while loop but there is an error.

You have to make it look like that

do {
//something
//something
}while(condition);

You can read more about this loop here : https://www.javatpoint.com/java-do-while-loop

0
On

You got some issues with your do while loops.

The do on line and 19 and on line 29 never gets closed and is missing the while clause at the end. Besides that you are missing 4 curly braces.

public class Paint1 {

    public static void main(String[] args) {
        //...
        do {
            //...
            while (!scnr.hasNextInt()) {
                //..
            }
            while (wallHeight < 0) {
                // ..
                do {
                    // ..
                    while (!scnr.hasNextDouble()) {
                    }
                    while (wallWidth < 0) {
                    }
                } while (true); // Should have a clause, or will be an infinite loop
            }
        } while (true); // Should have a clause, or will be an infinite loop
    }
}