CSC 143 A useful technique for catching typing errors is to
CSC 143
A useful technique for catching typing errors is to use a check digit. For example, International Article Number 13 (EAN 13) uses the last digit (also known as checkdigit) to validate the correctness of the first 12 digits. For more info on EAN 13. see: http://en.wikipedia.org/wikki/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. 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 EAN 13 numbers, untill the user decides to exit the program, by entering \'Q\' or \'Quit. Your program is allowed to use any of the concepts covered through the first 8 Chapters and topics from the appendices from our class text book.Solution
import java.util.*;
 import java.io.*;
 public class HelloWorld{
   
     //fucntion to check for EAN number
     public static boolean isEAN(String number)
     {
         //declare required v ariables
         int sum=0;
         int digit=0;
         int newSum=0;
         //get last digit
         int firstDigit=Character.getNumericValue(number.charAt(12));
       
         //loop through digits and get the sum
         for(int i=0;i<number.length()-1;i++)
         {
             digit=Character.getNumericValue(number.charAt(i));
             if(i%2==0)
             sum+=digit*1;
             else
             sum+=digit*3;
         }
       
         //check if difference is equal to last digit
         newSum=(10 - sum % 10) + sum;
         if(Math.abs(newSum-sum)==firstDigit)
         return true;
         return false;
     }
     public static void main(String []args){
          boolean flag=true;
        
          //loop until until user enters Q
     do{
         //prompt user for input
         System.out.print(\"Enter a EAN number: \");
         String number;
         Scanner input=new Scanner(System.in);
         number=input.next();
       
         //check if user entered Q
         //if yes,exit
         if(number.equals(\"Q\"))
         flag=false;
         //else check if user entered a valid number
         else if(number.length()!=13)
         {
             System.out.println(\"Enter a valid number\");
         }
         //if valid number entered, pcall EAN method
         else
         {
         if(isEAN(number))
         System.out.println(number+\" is EAN \");
         else
         System.out.println(number+\" is not EAN\");
         }
      }while(flag);
 }
 }
Sample Output:
Enter a EAN number: q213
4Enter a valid number
Enter a EAN number: 133456768
9Enter a valid number
Enter a EAN number: 4006381333931
4006381333931 is EAN
Enter a EAN number: 400638133930
12Enter a valid number
Enter a EAN number: 4006381333930
4006381333930 is not EAN
Enter a EAN number: Q


