Advertisement
Guest User

by

a guest
Jan 21st, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ByTheBook {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         //scanner and loop for number of cases (always the first line of input)
  8.         Scanner input = new Scanner(System.in);
  9.         int numCases = Integer.parseInt(input.nextLine());
  10.         for(int i = 0; i<numCases; i++)
  11.         {
  12.             //takes in the nth case
  13.             String str = input.nextLine();
  14.             //initalizes and resets the sum of the characters vaules for each case
  15.             int sum = 0;
  16.             for (int j = 0; j<str.length()-1; j++) //here i access the 10th character/9th index and parse to an integer i believe? is there a "parse-exception"
  17.             {
  18.                 //each character is multiplied by their weight
  19.                 //weight is determined by position of character, first characters weight is 10, second weight = 9... last weight = 2
  20.                 int weight = 10 - j;
  21.                 int digit = Integer.parseInt(str.substring(j,j+1));
  22.                 int product = weight*digit;
  23.                 sum += product;
  24.             }
  25.             //determines the difference between the sum of the series and the nearest multiple of 11    
  26.             int diff = sum%11;
  27.             int checkDigit = 11-diff;
  28.             if ((str.substring(9).equals("X") && checkDigit == 10)||(Integer.parseInt(str.substring(9)) == checkDigit))
  29.             {
  30.                 System.out.println("VALID");
  31.             }
  32.             else
  33.             {
  34.                 System.out.println("INVALID");
  35.             }
  36.     }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement