Java Barcode Code

Asked

Viewed 1,007 times

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 digits of the code of a given product and then returns the information indicating that the code is valid or not. Consider that the user will be able to release as many product codes as he wants.

1 answer

3


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

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

Add this result to 1 and then multiply by 10, after all subtract the stored result with that last result and "voilà", if the last number matches 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.