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 4 of 4

Thread: "possible loss of precision, found double, required int" HELP

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "possible loss of precision, found double, required int" HELP

    I need these variables to be doubles but it keeps saying an integer is required. My code does not have one integer so I cant have declared something wrong anywhere. Am I doing something wrong in the switch statement?

    Here's my code:

    /**	Lotto.java
       *	Programming Project, NCI
       *
       *	Date: 9 Nov 2011
       *	@author Katie
       *
       */
     
    import javabook.*;
     
    class LottoApp
    {
    	public static void main (String args[]){
     
    	//Declare variables
    	double money;
    	double bet;
    	double a;
    	double b;
    	double c;
    	double d;
    	double e;
    	double f;
    	double g;
    	double h;
    	double i;
    	double j;
    	double k;
    	double l;
    	double m;
    	double n;
    	double o;
    	double p;
     
     
    	//Declare objects
    	MainWindow mWindow;
    	InputBox iBox;
    	OutputBox oBox;
     
     
    	//Create Objects
    	mWindow = new MainWindow();
    	iBox = new InputBox(mWindow);
    	oBox = new OutputBox(mWindow);
     
     
     
    	//Use objects
    	mWindow.show();
    	oBox.show();
     
    	//get Input
    	money = iBox.getDouble("Please enter an amount. Note that one line costs 0.50 cent");
    	bet = iBox.getDouble("How much would you like to bet?");
    	a = 0;
    			b = 0;
    			c = 0;
    			d = 0;
    			e = 0;
    			f = 0;
    			g = 0;
    			h = 0;
    			i = 0;
    			j = 0;
    			k = 0;
    			l = 0;
    			m = 0;
    			n = 0;
    			o = 0;
    		p = 0;
     
     
     
    	//process
    	if (bet >= 2.00)
    				{
    					a = iBox.getDouble("enter first number");
    					b = iBox.getDouble("enter second number");
    					c = iBox.getDouble("enter third number");
    					d = iBox.getDouble("enter fourth number");
    					e = iBox.getDouble("enter fifth number");
    					f = iBox.getDouble("enter sixth number");
    					g = iBox.getDouble("enter seventh number");
    					h = iBox.getDouble("enter eighth number");
    					i = iBox.getDouble("enter ninth number");
    					j = iBox.getDouble("enter tenth number");
    					k = iBox.getDouble("enter elleventh number");
    					l = iBox.getDouble("enter twelfth number");
    					m = iBox.getDouble("enter thirteenth number");
    					n = iBox.getDouble("enter fourteenth number");
    					o = iBox.getDouble("enter fifteenth number");
    					p = iBox.getDouble("enter sixteenth number");
    				}
     
    				else if (bet >= 1.50)
    					{
    					a = iBox.getDouble("enter first number");
    					b = iBox.getDouble("enter second number");
    					c = iBox.getDouble("enter third number");
    					d = iBox.getDouble("enter fourth number");
    					e = iBox.getDouble("enter fifth number");
    					f = iBox.getDouble("enter sixth number");
    					g = iBox.getDouble("enter seventh number");
    					h = iBox.getDouble("enter eighth number");
    					i = iBox.getDouble("enter ninth number");
    					j = iBox.getDouble("enter tenth number");
    					k = iBox.getDouble("enter elleventh number");
    					l = iBox.getDouble("enter twelfth number");
    					}
     
    			else if (bet >= 1.00)
    				{
    					a = iBox.getDouble("enter first number");
    					b = iBox.getDouble("enter second number");
    					c = iBox.getDouble("enter third number");
    					d = iBox.getDouble("enter fourth number");
    					e = iBox.getDouble("enter fifth number");
    					f = iBox.getDouble("enter sixth number");
    					g = iBox.getDouble("enter seventh number");
    					h = iBox.getDouble("enter eighth number");
    				}
     
    			else if (bet >= 0.50)
    				{
    					a = iBox.getDouble("enter first number");
    					b = iBox.getDouble("enter second number");
    					c = iBox.getDouble("enter third number");
    					d = iBox.getDouble("enter fourth number");
     
    		}
    			else
    			{
    				oBox.print("You have insufficient funds");
    			}
     
     
     
    	//Output
     
    	switch (bet)
    		{
     
    		case 2:
    		oBox.print(" The numbers you have chosen are: " + a + ", "  + b + ", "  + c + ", "  + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + i + ",                  " + j + ", " + k + ", " + l + ", " + m + ", " + n + ", " + o + " & " + p );
    		break;
     
    		case 150:
    		oBox.print(" The numbers you have chosen are: " + a + ", "  + b + ", "  + c + ", "  + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + i + ",                 " + j + ", " + k + " & " + l );
    		break;
     
    		case 1:
    		oBox.print(" The numbers you have chosen are: " + a + ", "  + b + ", "  + c + ", "  + d + ", " + e + ", " + f + ", " + g + " & " + h );
    		break;
     
    		case 50:
    		oBox.print(" The numbers you have chosen are: " + a + ", "  + b + ", "  + c + " & "  + d );
    		break;
     
    		default:
    		oBox.print("Invalid Entry");
    		break;
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: "possible loss of precision, found double, required int" HELP

    Can you post the text of the error message that shows the source line with the problem?

    You can get rid of some of the error messages by using a cast to tell the compiler that you want to convert the double to int.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "possible loss of precision, found double, required int" HELP

    java.jpg

    Here's a screenshot of the error. As you can see I've tried what you said and type casted it and still no joy


    /**	Lotto.java
       *	Programming Project, NCI
       *
       *	Date: 9 Nov 2011
       *	@author Katie Donohoe
       *
       */
     
    import javabook.*;
    import java.util.Random;
     
    class Lotto2
    {
    	public static void main (String args[]){
     
    	//Declare variables
    	double money;
    	int money1 = (int)money;
    	double bet;
    	int bet1 = (int)bet;
    	int a;
    	int b;
    	int c;
    	int d;
    	int e;
    	int f;
    	int g;
    	int h;
    	int i;
    	int j;
    	int k;
    	int l;
    	int m;
    	int n;
    	int o;
    	int p;
     
     
    	//Declare objects
    	MainWindow mWindow;
    	InputBox iBox;
    	OutputBox oBox;
     
     
    	//Create Objects
    	mWindow = new MainWindow();
    	iBox = new InputBox(mWindow);
    	oBox = new OutputBox(mWindow);
     
     
     
    	//Use objects
    	mWindow.show();
    	oBox.show();
     
    	//get Input
    	money = iBox.getDouble("Please enter an amount. Note that one line costs 0.50 cent");
    	bet = iBox.getDouble("How much would you like to bet?");
    	a = 0;
    	b = 0;
    	c = 0;
    	d = 0;
    	e = 0;
    	f = 0;
    	g = 0;
    	h = 0;
    	i = 0;
    	j = 0;
    	k = 0;
    	l = 0;
    	m = 0;
    	n = 0;
    	o = 0;
    	p = 0;
     
     
     
    	//process
    	if (money >= 2.00 && bet == 2.00)
    				{
    					a = iBox.getInteger("enter first number");
    					b = iBox.getInteger("enter second number");
    					c = iBox.getInteger("enter third number");
    					d = iBox.getInteger("enter fourth number");
    					e = iBox.getInteger("enter fifth number");
    					f = iBox.getInteger("enter sixth number");
    					g = iBox.getInteger("enter seventh number");
    					h = iBox.getInteger("enter eighth number");
    					i = iBox.getInteger("enter ninth number");
    					j = iBox.getInteger("enter tenth number");
    					k = iBox.getInteger("enter elleventh number");
    					l = iBox.getInteger("enter twelfth number");
    					m = iBox.getInteger("enter thirteenth number");
    					n = iBox.getInteger("enter fourteenth number");
    					o = iBox.getInteger("enter fifteenth number");
    					p = iBox.getInteger("enter sixteenth number");
    				}
     
    				else if (money >= 1.50 && bet == 1.50)
    					{
    					a = iBox.getInteger("enter first number");
    					b = iBox.getInteger("enter second number");
    					c = iBox.getInteger("enter third number");
    					d = iBox.getInteger("enter fourth number");
    					e = iBox.getInteger("enter fifth number");
    					f = iBox.getInteger("enter sixth number");
    					g = iBox.getInteger("enter seventh number");
    					h = iBox.getInteger("enter eighth number");
    					i = iBox.getInteger("enter ninth number");
    					j = iBox.getInteger("enter tenth number");
    					k = iBox.getInteger("enter elleventh number");
    					l = iBox.getInteger("enter twelfth number");
    					}
     
    			else if (money >= 1.00 && bet == 1.00)
    				{
    					a = iBox.getInteger("enter first number");
    					b = iBox.getInteger("enter second number");
    					c = iBox.getInteger("enter third number");
    					d = iBox.getInteger("enter fourth number");
    					e = iBox.getInteger("enter fifth number");
    					f = iBox.getInteger("enter sixth number");
    					g = iBox.getInteger("enter seventh number");
    					h = iBox.getInteger("enter eighth number");
    				}
     
    			else if (money >= 0.50 && bet == 0.50)
    				{
    					a = iBox.getInteger("enter first number");
    					b = iBox.getInteger("enter second number");
    					c = iBox.getInteger("enter third number");
    					d = iBox.getInteger("enter fourth number");
     
    		}
    			else
    			{
    				oBox.print("You have insufficient funds");
    			}
     
     
    	//Output
     
    	switch (bet)
    		{
     
    		case 2.00:
    		oBox.println(" -- YOUR CHOSEN NUMBERS -- ");
    		oBox.println(" Line One: " + a + ", "  + b + ", "  + c + "& "  + d);
    		oBox.print(" Line Two: " + e + ", " + f + ", " + g + "& " + h);
    		oBox.print(" Line Three: " + i + ", " + j + ", " + k + "& " + l);
    		oBox.print(" Line Four: " + m + ", " + n + ", " + o + " & " + p );
    		break;
     
    		case 1.50:
    		oBox.print(" -- YOUR CHOSEN NUMBERS -- ");
    		oBox.print(" Line One: " + a + ", "  + b + ", "  + c + "& "  + d);
    		oBox.print(" Line Two: "  + e + ", " + f + ", " + g + "& " + h);
    		oBox.print(" Line Three: " + i + ", " + j + ", " + k + " & " + l );
    		break;
     
    		case 1.00:
    		oBox.print(" -- YOUR CHOSEN NUMBERS -- ");
    		oBox.print(" Line One: " + a + ", "  + b + ", "  + c + " & "  + d );
    		oBox.print(" Line Two: " + e + ", " + f + ", " + g + " & " + h );
    		break;
     
    		case 0.50:
    		oBox.println(" -- YOUR CHOSEN NUMBERS -- ");
    		oBox.println(" Line One: " + a + ", "  + b + ", "  + c + " & "  + d );
    		break;
     
    		default:
    		oBox.println("Invalid Entry");
    		break;
    		}
     
    	}
     
    }
    Last edited by kkatchh; November 6th, 2011 at 10:40 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: "possible loss of precision, found double, required int" HELP

    Did you do the cast where the error is happening?

    Where do you use the bet1 variable? You set bet1 to the value of bet before you assign a value to bet.
    Don't you want to use the current value of bet and not the initial value?
    The cast you have was done when the class was created. The value that bet has then is not related to the value of bet when your program executes.

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Possible loss of precision (double/int)
    By haloboy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 02:23 AM
  3. [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
  4. [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
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM