Java Barcode

1

How to do a barcode validation in Java? A Java application that simulates a barcode reader. The program prompts the user to enter the 13-digit code of a particular product and then returns the information indicating that the code is valid or not. Consider that the user can post as many product codes as he wants.

    
asked by anonymous 15.09.2015 / 00:35

1 answer

3

Any last barcode number would be the result of an account with the other previous numbers. So store the 13, 12 or 11 digits in a vector using strings and then convert them to integers.

After that, make all numbers in odd positions * 1 and numbers in even positions * 1 (that is, after that, add all the results, store this result in another variable and divide by 10, (if you use double pass to integer).

Add this result to 1 and then multiply by 10, after that all subtract the stored result with that last result and "voilà", if the last number hits the account then this barcode is correct if it is not incorrect.

int i = 0;
    double code[] = new double[13];
    double total = 0;
    for(i = 0;i < 12;i++)
    {
        codigo_barra += cody[i];  

        code[i] = Integer.parseInt(cody[i]);


            if(i%2== 0)
            {
                code[i] *= 3;                  
            }          


       total += code[i];
    }   

    double t = total;
    total /= 10;
    int to = (int)total;

    to += 1;
    to *=10; 
    to -= t;


    if(to == code[13])
    {
        valida = "Codigo Correto";            
    }
    else
    {
        valida = "Codigo Incorreto";
    }

    System.out.println("Resultado: "+to);
    
15.09.2015 / 02:51