-1

I am a beginner in Java language and I am getting problem with code which has the 'incompatible types: possible lossy conversion from double to int'. What can i do to fix this? I am totally lost it.

import javax.swing.*;
import java.text.*;

public class CalculateIncome{

    public static void main(String[]args){

        String s1, outMessage;
        double monthlySales, income;

        DecimalFormat num = new DecimalFormat(",###.00");

        s1 = JOptionPane.showInputDialog("Enter the value of monthly sales:");
        monthlySales = Double.parseDouble(s1);

        switch(monthlySales) {
        case 1:
            income = 200.00+0.03*monthlySales;
            break;
        case 2:
            income = 250.00+0.05*monthlySales;
            break;
        case 3:
            income = 300.00+0.09*monthlySales;
            break;
        case 4:
            income = 325.00+0.12*monthlySales;
            break;
        case 5:
            income = 350.00+0.14*monthlySales;
            break;
        case 6:
            income = 375.00+0.15*monthlySales;
        default:
            outMessage = ("For the monthly sales of $" +num.format(monthlySales)+ "\nThe income is"+num.format(income));
        }  
            JOptionPane.showMessageDialog(null,outMessage,"QuickTest Program 5.5b", JOptionPane.INFORMATION_MESSAGE);

            System.exit(0);
    }

        }
Stultuske
  • 9,296
  • 1
  • 25
  • 37
Miru
  • 1
  • 6

2 Answers2

1

Basically this switch-statement causes the problem:

switch(monthlySales){
    case 1:
        ...

It converts monthlySales from double to int. Since double is a 64-bit float, while int is a 32-bit Integer, it's quite likely that the conversion from double to int isn't lossless.

Either use int for monthlySales, which is my suggested solution, since you can simply change the unit from dollar to cent, or whatever currency you're calculating in.
Another option would be to add a cast: switch((int) monthlySales){ (not recommended, due to the same loss of information the error warns you about - the fractional part will simply be removed from the value, and double can hold way larger and smaller values than int).
And last but not least: replace the switch-statement, with if-else and compare with a double: if(monthlySales == 1.0)....

  • oh i see.. thank you so much for clearing things up for me :) – Miru Oct 05 '15 at 11:04
  • I would suggest to not use casting of double to int, it may work in this simple code, but on larger scale it may introduce bugs, since casting double to int always removes the decimal and rounds toward zero. So ¨1.4 and 1.5 are both 1 when cast. Fast forward few years, and the OP is scratching his head why his code misbehaving without him seeing any reason. Other suggestions are okay. – The Law Oct 05 '15 at 11:08
1

In Java language, switch expression allows int datatype. Upon supplying other datatypes, attempt will be made to automatically promote the value to an int. Since you are supplying a value of type double, It is being casted to an int and hence the warning.

    double monthlySales, income;

    switch(monthlySales) {

Monthly sales parameter will be an integer unless, amount of money or the likes are being captured. I would suggest

    int monthlySalesInt = 0;
    try {
        int monthlySalesInt = Integer.parseInt(monthlySales);
    }catch(NumberFormatException e) {
        //exception handling - either wrap and rethrow, or warn using JDialog.
    }
    switch(monthlySalesInt) {
    ...
deepak marathe
  • 409
  • 3
  • 10