Write a Java program that asks the user for a EAN13 and vali

Write a Java program that asks the user for a EAN13 and validates it as either correct or incorrect (display a message accordingly). Your program should continue to run, to validate multiple EAN13 numbers, until the user decides to exit the program, by entering \'Q\' or \'Quit\'.

Comment your code and use procedural decomposition problem solving techniques.

For more info on EAN13, see: http://en.wikipedia.org/wiki/International_Article_Number_(EAN) .

If EAN13 is 4006381333931 the check code is computed as follows:

The nearest multiple of 10 that is equal or higher than the sum, is 90. Subtract them: 90 - 89 = 1, this is the last digit of the barcode.

Calculation
First 12 digits of the barcode 4 0 0 6 3 8 1 3 3 3 9 3
Weights 1 3 1 3 1 3 1 3 1 3 1 3
Multiplied by weight 4 0 0 18 3 24 1 9 3 9 9 9
Sum 89

Solution

import java.util.*;

class EAN13
{
   public static void main (String[] args)
   {
       int digit,lastdigit,weight,diff,sum;
       String option =\"yes\";
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the first 12 digits of EAN13\");
      
       do
       {
       sum =0;
       for(int i=1;i<=12;i++)
       {
           digit = scan.nextInt(); // input first 12 digits
           if(i%2 == 0) // assign weights 3 to even digits and 1 to odd digits
           weight = 3;
           else
           weight = 1;
           sum = sum + digit*weight; // sum all the digits and their weights
       }
      
       System.out.println(\"sum = \"+sum);
       System.out.println(\"Enter the last digit of EAN13\");
       lastdigit = scan.nextInt();
      
       diff = 10 - (sum % 10); // compute the difference of the sum and the nearest multiple of 10
      
       if (diff == lastdigit) //validate using 13th digit in EAN13
       System.out.println(\"EAN13 is validated and correct\");
       else
       System.out.println(\"EAN13 is not validated and is incorrect\");
      
       System.out.println(\"Do you want to check another EAN13?\");
       option = scan.next();
       if(option.equals(\"Quit\"))
       break;
      
   }while(!option.equals(\"Quit\"));
      
      
   }
  
}

output:

Write a Java program that asks the user for a EAN13 and validates it as either correct or incorrect (display a message accordingly). Your program should continu
Write a Java program that asks the user for a EAN13 and validates it as either correct or incorrect (display a message accordingly). Your program should continu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site