Possible loss of precision (double/int)
Ok I know the problem is in the gross section but I dont know why that plus sign is giving me an issue here is the code:
Code Java:
import java.util.Scanner;
public class Lab2
{
public static void main(String[] args)
{
System.out.println("Enter hourly wage");
System.out.println("Enter hours worked");
int gross;
int wage;
int hours;
while(hours > -1)
{
if (hours > 40)
{
gross =(40*wage)+(1.5*wage*(hours-40));
System.out.println(gross);
}
else
gross = (wage*hours);
System.out.println(gross);
}
}
The compile error says "possible loss of precision"
gross =(40*wage)+(1.5*wage*(hours-40));
^
found: double
required: int
I am new to this stuff but I know that I never declared anything in the code as double
Re: Possible loss of precision (double/int)
There are two primary ways computers represent numbers: as an integer (i.e. -1,0,1,2,3,...), or as a floating point number (1.23, 1.5, -1.5, etc.).
In Java, the compiler will automatically promote/cast up any integer types into a floating number data type (either float or double) when the operation contains one or more floating point numbers.
In your code, you have a 1.5 in the middle, which would automatically promote the results to a floating point number (with double precision).
However, when you try to store the number into an integer data type, their must be some loss of data present to facilitate that mechanism because integers can't store any decimals/exponents (basically everything after the decimal point gets chopped off/truncated). The Java compiler has decided that this is generally an undesirable effect and is telling you that. However, you can specify an explicit cast to int which will tell the Java compiler that you know what you're doing (or at least are pretending to know what you're doing) and allow the cast to go through anyways. The better solution though might be to use a floating point variable.
Code Java:
double gross; // instead of int gross
// or, if you want to leave gross as an integer, add an explicit cast to the calculation:
gross =(int)((40*wage)+(1.5*wage*(hours-40)));
Re: Possible loss of precision (double/int)
Thanks for the reply,the error is now gone.I forgot about floating point.I used the example you gave and type-casted gross from a double to an int.However I have 3 errors in the program saying variables "hours" and "wage" might have not been initialized.These three errors occur in:
if (hours > 40)
---^
gross =(40*wage)+(1.5*wage*(hours-40));
--------------^
gross = (wage*hours);
-----------^
I declared both "wage" and "hours" as int and even tried double but I still get this error.
Re: Possible loss of precision (double/int)
declared != initialized
declared means you simply state that the variable exists. Initialized means that you actually assigned a value to that variable.
In Java, you must both declare and initialize variables before you can use them (note that assigning a value to that variable is not the same as using it). While you don't need need to initialize a variable at the same time you declare it, it's often a good practice to do so.
Code Java:
int a; // variable a is declared but not initialized
// ... some time later
a = 5; // a is now initialized and can be used
int b = a; // variable b is both declared and initialized
In your code, you're missing the part which actually retrieves input from the user one what the wage and hours worked is. Place that in there and assign the values to wage and hours, and that should fix the problems you're getting.
Re: Possible loss of precision (double/int)
I have initialized the variables and the program is almost complete!However my answer results in an infinite loop and I dont know why.The answer is correct but no matter where I put the output it is reapeating.Here is the modified code.
Code Java:
import java.util.Scanner;
public class Lab2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double gross;
int wage,hours;
System.out.println("Enter hourly wage");
wage = keyboard.nextInt( );
System.out.println("Enter hours worked");
hours = keyboard.nextInt( );
while(hours > -1)
{
if (hours > 40)
{
gross =(int)((40*wage)+(1.5*wage*(hours-40)));
System.out.println("Your total gross pay is:"+" " +gross+" "+ " Dollars");
}
else
gross = (wage*hours);
System.out.println("Your total gross pay is:"+" " +gross+" "+ " Dollars");
}
}
}
Re: Possible loss of precision (double/int)
You're not re-asking the user to input a new hours worked in the while loop. You can either ask the user for a new hours worked at the end of the while loop, or remove the while loop all together.
Re: Possible loss of precision (double/int)
Threw 2 "break;" statements in there and she works like a charm!Thanks for the help helloworld922!I learned a lot about initialization and precision thanks to your help.