Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Possible loss of precision (double/int)

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:



    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
    Last edited by helloworld922; March 8th, 2011 at 12:40 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    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)));

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by haloboy; March 8th, 2011 at 01:27 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    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.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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");
     
     
    		}
    	}
    }
    Last edited by helloworld922; March 8th, 2011 at 02:20 AM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. set Precision
    By sabir in forum Java Theory & Questions
    Replies: 1
    Last Post: March 4th, 2011, 11:32 AM
  2. Streaming Raw Data to a Client - huge message loss
    By MarkusTandy in forum Java Networking
    Replies: 0
    Last Post: February 19th, 2011, 07:41 PM
  3. My code is having error, may be a lost of precision
    By siaosiaoboii in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 31st, 2011, 07:42 AM
  4. [SOLVED] Loss of Precision (Double/Int)
    By Scotty in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2010, 01:45 PM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM