Modulus Problem

Hi, i have had a look through the forums and i can't find anything along these lines.

Basically i am trying to generate a hash code using the modulus function but for some reason i am not getting the expected value back (or i am expecting the wrong value). Could someone tell me where im going wrong...heres the code:

public static int hash(int h) {

h = h % 20;

return h;

}

if i set h to be 2 then the returned value is 2. i was expecting 0 as there are no remainders when 20 is divided by 2. another example is when h = 6, it just returns 6 as if it isn't even running the modulus function.

Thanks, Nick

[655 byte] By [MysteriousTeda] at [2007-9-30 2:23:31]
# 1

> public static int hash(int h) {

>h = h % 20;

>return h;

As far as i remember the modulus function, you've got it mixed up. anything modulo 20 will return the remainder from 20, hence 2 modulo 20 is 2, and 22 mod 20 would be 2.

I think you meant to have it as "h = 20 % h;" is you were looking to have 0 as the answer from 20 mod 2.

SullyBluea at 2007-7-16 13:32:52 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Yep thats its. cheers sullyblue
MysteriousTeda at 2007-7-16 13:32:52 > top of Java-index,Archived Forums,New To Java Technology Archive...