Results 1 to 3 of 3

Thread: Converting Java snippet to VBA

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Converting Java snippet to VBA

    I have the following Java code:
    Code:
    public static void main(String [ ] args)
    	{
    		String isbn = "9781590387474";
    		if (isbn.length() == 13 && isbn.indexOf("978") == 0)
    		{
    		  isbn = isbn.substring(3,12);
    		  int xsum = 0;
    
    		  for (int i = 0; i < 9; i++)
    		  {
    		      xsum += (10 - i) * Character.getNumericValue(isbn.charAt(i));
    		  }
    
    		  xsum %= 11;
    		  xsum = 11 - xsum;
    
    		  String x_val = String.valueOf(xsum);
    
    		  switch (xsum)
    		  {
    		      case 10: x_val = "X"; break;
    		      case 11: x_val = "0"; break;
    		  }
    
    		  isbn += x_val;
    		}
    		System.out.println(isbn);		
    	}
    I tried converting it to VBA myself, and got this:
    Code:
    Function UPCtoISBN(strUPCin As String) As String
        Dim strTemp As String
        If Len(strUPCin) = 13 And Left(strUPCin, 3) = "978" Then
            
            strTemp = Mid(strUPCin, 3, 10)
            Dim xsum As Integer
            xsum = 0
            For x = 1 To 9
                xsum = xsum + ((10 - x) * Mid(strTemp, x, 1))
            Next x
            xsum = xsum Mod 11
            xsum = 11 - xsum
            Select Case xsum
                Case 10
                    strTemp = strTemp & "X"
                Case 11
                    strTemp = strTemp & "0"
            End Select
            
        End If
        UPCtoISBN = strTemp
    End Function
    Unfortunately, it does not work.

    Anyone willing to help fix it?

    Thanks!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Re: Converting Java snippet to VBA

    Well, I changed it to this:
    Code:
    Function UPCtoISBN(strUPCin As String) As String
        Dim strTemp As String
        If Len(strUPCin) = 13 And Left(strUPCin, 3) = "978" Then
            
            strTemp = Mid(strUPCin, 4, 9)
            Dim xsum As Integer
            xsum = 0
            For x = 1 To 9
                xsum = xsum + ((10 - x + 1) * Mid(strTemp, x, 1))
            Next x
            xsum = xsum Mod 11
            xsum = 11 - xsum
            Select Case xsum
                Case 10
                    strTemp = strTemp & "X"
                Case 11
                    strTemp = strTemp & "0"
                Case Else
                    strTemp = strTemp & xsum
            End Select
            
        End If
        UPCtoISBN = strTemp
    End Function
    and it worked for all the books I checked.

    Anyone care to try a few books of their own for me to validate?

    Thanks!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Converting Java snippet to VBA

    It would be best to separate calculation of check digit, e.g. another package/class for check digit calculation.

Posting Permissions

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



Click Here to Expand Forum to Full Width