Search code examples
javaintegerdoubletype-mismatch

"Type mismatch: cannot convert from double to int Error"


import java.util.Scanner; 

public class PaintCostCalculator {
   public static void main( String args[] )
   {
    try (Scanner input = new Scanner(System.in)) {

    //variable declarations
    int NoOfRooms;
    int RoomCounter;
    int choice;
    int Area = 0;
    int AreaSum = 0;
    int TotalLSCost;
    int TotalSGCost;
    int TotalMatCost;

    
    //constants declarations
    final int PaintCoverage = 16;
    final int LowSheenCost = 17.6;
    final int SemiGlossCost = 20;
    final int MatteCost = 14.3;
    
    //code
    System.out.print("Please enter the number of rooms to be painted: ");
    NoOfRooms = input.nextInt();
    for(RoomCounter = 0; RoomCounter < NoOfRooms; RoomCounter ++) {
        System.out.printf("\nEnter the area of room %d in m^2.: ", RoomCounter + 1);
        Area = input.nextInt();
        AreaSum = AreaSum + Area;
    }
    System.out.println("\nPlease choose one of the following paint options: \n1. Low Sheen($17.60/L)\n2. Semi Gloss($20/L)\n3. Matte($14.30/L)");
    choice = input.nextInt();
    switch (choice)
    {
        case 1: 
                System.out.print("You have chosen Low Sheen\n");
                TotalLSCost = (AreaSum / PaintCoverage) * LowSheenCost;
                System.out.printf("To paint a total area of %dm^2 with Low Sheen paint it would cost a total of %d", AreaSum, TotalLSCost);
                break;
        case 2: 
                System.out.print("You have chosen Semi Gloss\n");
                TotalSGCost = (AreaSum / PaintCoverage) * SemiGlossCost;
                System.out.printf("To paint a total area of %dm^2 with Semi Gloss paint it would cost a total of %d", AreaSum, TotalSGCost);
                break;
        case 3: 
                System.out.print("You have chosen Matte\n");
                TotalMatCost = (AreaSum / PaintCoverage) * MatteCost;
                System.out.printf("To paint a total area of %dm^2 with Matte paint it would cost a total of %d", AreaSum, TotalMatCost);
                break;
    }
    }
   }
}

I'm still in early learning stages of Java and it's my first language, trying to exercise program tasks. Simple program to ask user for number of rooms, area of each of the rooms than offers choice of 3 paint options to which it will calculate the total area to be painted and price of paint required. I'm getting the error:

Type mismatch: cannot convert from double to int


Solution

  • final int LowSheenCost = 17.6;
    

    17.6 is a double literal: it has a whole-number part and a fractional part. You can't put that into a variable that's expecting a whole number.

    Instead:

    final double LowSheenCost = 17.6;
    

    (Similarly for others).


    A little gotcha is that this:

    final int PaintCoverage = 16;
    

    is valid; but it will probably give you unexpected results later, because then this:

    (AreaSum / PaintCoverage)
    

    in

    TotalLSCost = (AreaSum / PaintCoverage) * LowSheenCost;
    

    is integer division, that is, you will only get a whole-number result for the division: if |AreaSum| < 16, its value will be zero; if 16 <= AreaSum < 32, its value will be one etc.

    So, also make PaintCoverage a double instead of an int if you intend to do fractional division.