DSAK
DSAK

Reputation: 13

I'm missing a break statement in my IF statement

I'm currently working on a class task involving calculating the amount of paint required for any number of walls with individual dimensions, the thing is that I've set it up everything and all calculations and everything work out correctly.

I'm using an IF statement to divert the EU to the correct code block but for some reason when I use the feet to imperial gallons section, it still tries to run the metres to litres section as well. I'm clearly missing something at the end of my the first IF statement and DO WHILE loop (loops used to calculate total area of walls and the over area of the room) I was just wondering if someone could cast an quick eye over it and point me in the right direction. Attached below is the code if any of you could help.

(apologies for any discrepiencies first time posting here so be kind lol)

public static void main(String[] args) throws IOException
{
        double widthOfWall = 0;
        double lengthOfWall = 0;
        double areaOfWall = 0; 
        double areaOfRoom = 0;
        double paintNeeded = 0;
        char   UnitsUsed = ' '; 
        char   extraCalc = 0;
        
        
        
    //Scanner class
    Scanner keyboard = new Scanner (System.in);
    
    {   
    System.out.println("Are using Feet or Metre? (F/M)");
    UnitsUsed = ValidateData.checkTwoChars('F', 'M');
    {
        {   
if (UnitsUsed == 'F')
    
    do{ 
        System.out.println("Please enter width of the wall");
        widthOfWall = keyboard.nextDouble();
        
        System.out.println("Please enter the Length of the wall");
        lengthOfWall = keyboard.nextDouble();
        
        areaOfWall = widthOfWall * lengthOfWall; 
        System.out.println("The area of this wall is " + areaOfWall);
        
        System.out.println("Do you require an additional calculation? ");
        extraCalc = ValidateData.checkTwoChars('Y', 'N'); 
        
        areaOfRoom += areaOfWall;
        areaOfWall ++; 
        
        
System.out.println("The area of the room " + areaOfRoom);
        
        
        paintNeeded = areaOfRoom / 6.229; 
        System.out.println("The amount of paint needed (in Imperial Gallons) " + paintNeeded);
        }
        while (extraCalc == 'Y'); 
        

else if (UnitsUsed == 'M');
    
    //Do while loop
    do{ 
    System.out.println("Please enter width of the wall");
    widthOfWall = keyboard.nextDouble();
    
    System.out.println("Please enter the Length of the wall");
    lengthOfWall = keyboard.nextDouble();
    
    areaOfWall = widthOfWall * lengthOfWall; 
    System.out.println("The area of this wall is " + areaOfWall);
    
    System.out.println("Do you require an additional calculation? ");
    extraCalc = ValidateData.checkTwoChars('Y', 'N'); 
    
    areaOfRoom += areaOfWall;
    areaOfWall ++; 
    }
    while (extraCalc == 'Y'); 
    
    System.out.println("The area of the room " + areaOfRoom);
    
    paintNeeded = areaOfRoom / 12; 
    System.out.println("The amount of paint needed (in litres) " + paintNeeded);
    }




}
}}

}

Upvotes: 0

Views: 41

Answers (1)

Tenfour04
Tenfour04

Reputation: 93639

You have a semicolon here instead of an opening brace {:

else if (UnitsUsed == 'M');

Also, that is a lot of repetitious code, when the only difference between the two branches is the units. You can set multiplier and units String variables using ternary operators, and then eliminate the if/else to simplify this.

Upvotes: 1

Related Questions