I have problem compile this. Line 23 having a problem ( possible loss precision, required int, found double).

public void actionPerformed( ActionEvent e )
	{
		try
		{
			// call a function to get the value - if an error occurs,
			// display a message box and set focus to the correct field
			double Limit = checkField( speedLimit, "" );
			double Travel = checkField( speedTravel, "" );
                        double Previous = checkField( tickets, "" );
                        double Over = Travel - Limit;
			double Cost = Over*20;

			DecimalFormat df = new DecimalFormat( "#,###,##0.00" );

                        speedOver.setText( "" +
                        df.format( Over ) );
			speedOver.setBackground( Color.YELLOW );

                        speedCost.setText( "" +
                        df.format( Cost ) );
			speedCost.setBackground( Color.PINK );

                        switch(Previous)
                        {
                            case 0:
                            courtCost.setText( "" +
                            df.format( 74.80 ) );
                            courtCost.setBackground( Color.GRAY );
                            break;
                            case 1:
                            courtCost.setText( "" +
                            df.format( 74.80 + 25 ) );
                            courtCost.setBackground( Color.GRAY );
                            break;
                            case 2:
                            courtCost.setText( "" +
                            df.format( 74.80 + 50 ) );
                            courtCost.setBackground( Color.GRAY );
                            break;
                        }

		    outputLabel.setText( "The average annual cost to operate this appliance is $" +
				df.format( Cost ) );
			outputLabel.setBackground( Color.YELLOW );
		}
}

Recommended Answers

All 3 Replies

You can't use doubles with switch statements. You can only use ints or chars. So it is saying possible loss of precision probably because it is automatically downcasting the double to an int.

I understand that, but how do I fix that?

When I change

double Previous = checkField( tickets, "" );

to

int Previous = checkField( tickets, "" );

it give me an error also.

You fix it by not using a switch statement with doubles. It just conceptually does not make sense. You can use if statements instead of a switch statement; if statements will work with doubles. For example,

double value = 1.1;
if (value >= 0.0 && value <= 2.0) doSomething();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.