0
votes

Is there a difference between the way that the Modulo operates calculates values in Java vs the way that it does in VBScript?

Both of these are returning a digit that I am using as part of a larger number later in the code but I believe the issue is between the way VBScript and Java are handling the Mod operator. I may be wrong though.

I am trying to work through reasons why I am seeing different outputs from when I run the below VBscript code block vs my replicated version in Java, sorry for the delay in updating the post.

The function takes in a String and then works to determine a return digit based upon the logic in the loop. The missing code just has to do with initializing the variables used, and determining the length of the string to loop over.

Any help would be greatly appreciated! Thank you

VBScript:

For i=1 to Length

CurrentNumber = Mid(CCNumber,i,1)
CurrentNumber = Int(CurrentNumber)

If (i mod 2) <> 0 then
    ModNumber = CurrentNumber * 2
    If ModNumber > 9 then
        Total = Total + 1 + (ModNumber mod 10)
    Else
        Total = Total + ModNumber
    End If
Else
    Total = Total + CurrentNumber
End If
Next

cd = ((Int(Total/10) + 1) * 10) - Total

if cd = 10 then cd = 0

CheckDigit = cd

Java:

for (i=0; i<length; i++)
     {
         String currentNumberString = CCNumber.substring(i,i+1);

         currentNumber = Integer.valueOf(currentNumberString);

         if (i % 2 != 0)
         {
             Integer ModNumber = currentNumber * 2;

             if (ModNumber > 9)
             {
                 total = total + 1 + (ModNumber % 10);
             }
             else
             {
                 total = total + ModNumber;
             }
         }
         else
         {
             total = total + currentNumber;
         }

     }

    int cd = ((Integer.valueOf(total/10) + 1) * 10) - total;

    if (cd == 10)
    {
        cd = 0;
    }

    return cd;
 } 
1
Is there reason you believe this to be the case? An example could help people better answer your question. - thatidiotguy
Is there some specific issue that you're troubleshooting? - Brian Driscoll
Remainder Operator in Java and here is the Mod Operator in VBScript (I'm assuming unexpected behavior in Negative numbers or Floating points perhaps?) - gtgaxiola

1 Answers

2
votes

One difference: The Mod operator in VBScript always returns an integer. Java's % operator can return a fractional value. So 5.2 Mod 2 evaluates to 1 in VBScript, but 5.2 % 2 evaluates to 1.2 in Java.


Edit: Based on your edit, this appears to be the Luhn algorithm. The only real problem with the Java code is a typo; nothing to do with the mod operator. Here you assign a variable currrentNumber (with a triple R):

currrentNumber = Integer.valueOf(currentNumberString);

Then you use a different variable (double R):

Integer ModNumber = currentNumber * 2;

Edit: Another difference is that because VBScript string indices start at 1, and Java string indices start at 0, the code is using different alternate digits. Either If (i mod 2) <> 0 should be If (i mod 2) = 0, or if (i % 2 != 0) should be if (i % 2 == 0), I'm not sure which.