-
Help with Arrays
Hello all,
I'm having a little trouble with this program, can't tell where I have went wrong any help would be appreciated.
I keep getting the error:
tenPercentOff(int) in DebugThree4 cannot be applied to (double)
here is the code:
public class DebugThree4
// This class discounts prices by 10%
{
public static void main(String args[])
{
int price = 100;
double price2 = 100.00;
tenPercentOff(price);
tenPercentOff(price2);
}
public static void tenPercentOff(int p)
{
double newPrice = p * .10;
System.out.println("Ten percent off");
System.out.println("New price is " + newPrice);
}
}
-
Re: Help with Arrays
[quote]
public static void main(String args[])
{
int price = 100;
[B]double[/B] price2 = 100.00;
tenPercentOff(price);
[I]A.[/I] [B]tenPercentOff(price2);[/B]
}
[I]B. [/I]public static void tenPercentOff([B]int [/B]p)
{[/quote]In the above program, you are passing an double to function that accepts an integer value. Since double is more precise than an integer (i.e its value is greater that int) hence Java does not do an implicit cast. If you cast the double to the int, you will loose info.
Eg: 100.90 (a double value) when cast to int will become 100
Thus when there is such loss, you either have to explicitly cast or change the method parameter to accept a double as follows
Change line A to
[B]tenPercentOff((int)price2);
OR
[/B]Change line B to
public static void tenPercentOff([B]double [/B]p).
Hope this helps.
Regards,
Sahil.
[SIZE=1][COLOR=Silver]--Smile, it makes people wonder what you are thinking!!!:)[/COLOR][/SIZE]