Results 1 to 7 of 7
  1. #1
    louisw is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Help with java division

    hey guys!
    this is my first post here so basically what i need help is with my code i am making a runescape calculator where you enter what fish you are fishing then how much exp you want but the sum gives the wrong answer e.g 20 (exp wanted) / 20 (xp of fish) and it gives the answer 2


    heres my code:
    Java Code:
    import java.util.Scanner;
    public class sharks
    {
    	static String shrimp;
    	static String fishingshrimp;
    	static String fishanswershrimp;
    	static String crayfish;
    	static String fishingcrayfish;
    	static String fishanswercrayfish;
    	static String karabwanji;
    	static String fishingkarabwanji;
    	static String fishanswerkarabwanji;
    	static String sardine;
    	static String fishingsardine;
    	static String fishanswersardine;
    	static String shark;
    	static String fishingshark;
    	static String fishanswershark;
    	static String rocktails;
    	static String fishingrocktails;
    	static String fishanswerrocktails;
    
    	
    	public static void main (String args[])
    	{
    		int xpwanted;
    		int answer;
    		int shrimpxp = 10;
    		int crayfishxp = 10;
    		int karabwanjixp = 5;
    		int sardinexp=20;
    		int sharkxp = 150;
    		int rocktailxp = 380;
    		shrimp = "shrimp";
    		fishingshrimp = "choiceshrimp";
    		fishanswershrimp = "shrimp";
    		crayfish = "crayfish";
    		fishingcrayfish = "choicecrayfish";
    		fishanswercrayfish = "crayfish";
    		karabwanji = "karabwanji";
    		fishingkarabwanji = "choicekarabwanji";
    		fishanswerkarabwanji = "karabwanji";
    		sardine = "sardine";
    		fishingsardine = "choicesardine";
    		fishanswersardine = "sardine";
    		shark = "shark";                   
    		fishingshark = "choiceshark";      
    		fishanswershark="shark";           
    		rocktails ="rocktails";            
    		fishingrocktails = "choicerocktails";      
    		fishanswerrocktails="rocktails";   
    		Scanner Fishing = new Scanner (System.in);
    		System.out.println("Enter what fish you are fishing: ");
    		shark = Fishing.nextLine();
    		if (fishanswershrimp.equals(shrimp))
    		{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer = xpwanted / shrimpxp;
    			System.out.println(answer);
    			System.exit(0);
    		}
    		if (fishanswercrayfish.equals(crayfish))
    		{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer = xpwanted / crayfishxp;
    			System.out.println(answer);
    			System.exit(0);
    		}
    		if (fishanswerkarabwanji.equals(karabwanji))
    		{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer = xpwanted / karabwanjixp;
    			System.out.println(answer);
    			System.exit(0);
    		}
    		if (fishanswersardine.equals(sardine))
    		{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer = xpwanted / sardinexp;
    			System.out.println(answer);
    			System.exit(0);
    		}
    		if (fishanswershark.equals(shark))
    				{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer   = xpwanted / sharkxp;
    			System.out.println(answer);
    			System.exit(0);
    				}
    		if (fishanswerrocktails.equals(rocktails))
    		{
    			System.out.println("Enter experience wanted to gain: ");
    			xpwanted = Fishing.nextInt();
    			answer = xpwanted/rocktailxp;
    			System.out.println(answer);
    			System.exit(0);
    		}
    		
    	}
    
    	static String cookedshrimp;
    
        public static void maincooking (String args[])
    {
    	
    }
    }
    cheers if you can help it would help alot!
    Last edited by sunde887; 09-28-2011 at 12:11 AM. Reason: Added code tags, [code]...[/code]

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Help with java division

    the sum gives the wrong answer
    Are you using integer arithmetic? For example 10/6 = 1 (no remainder) & 10/11 = 0

  3. #3
    louisw is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help with java division

    err im sorry but i dont know what that means as im fairly new to java but i do do the xp wanted/fish xp so yea i guess i do

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Help with java division

    If you want decimal places change either of the operands to a double or float.
    10.0/3 = 3.333333
    This can also be done by casting:
    (double)10/3

  5. #5
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Default Re: Help with java division

    Change "int answer" to "double answer" or "float answer" as Norm says. Double is more precise (ie: has more decimal places or a larger number which it can store) but requires 4 bytes of memory not 2, as float requires. (At least I think it is 4.)

  6. #6
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Default Re: Help with java division

    int by the way is the data type integer. (32 bit in C, probably also 32 bit in Java.) Look up Java data types. In fact, I'll do it for you. Here are the 'primitive' data types:

    Byte: 8bit/1Byte integer. Holds whole number values from -128 to +127
    Short: 16bit/2Byte integer. Holds whole number values from -32,768 to + 32,767
    Int: 32bit/4Byte integer. Holds whole numbers from/to +-2,147,483,648
    Long 64bit/8Byte integer. Holds whole nums +- 9,223,372,036,854,775,808

    Float: 32bit/2Byte (so I was right) floating point number. (Holds decimal points and exponents and stuff)
    Double: Same as float but 64bits of data. Can hold a bigger number and more DP's. (It has more precision.)
    Floating point arithmetic often chops off the very ends of numbers in division etc, so for example 10 divided by 3 might end up as 3.333333333749220, or something random like that. I don't know why the randomness appears, I'm not a computer scientist, look up the IEEE 754 industry standard for more info.

    Boolean: Either true (1) or false (0), but still takes up one byte of data, so just use int instead, perhaps.
    Char: 16bit Unicode character. Minimum value of 0 and max val of 65,535. This is different to C/C++.

  7. #7
    Rafting_Bob is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: Help with java division

    There is a bug on line 55 that always causes the first if condition to be entered. If you step through the code with a debugger or desk check it you should be able to find it. Good luck!

Similar Threads

  1. int division
    By überfuzz in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 07:10 PM
  2. Replies: 1
    Last Post: 03-09-2011, 03:45 PM
  3. Issues with Division
    By KnotsaboutWood in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 03:32 AM
  4. big double division
    By grilo in forum Advanced Java
    Replies: 4
    Last Post: 10-11-2009, 10:07 PM
  5. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •