Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ByTheBook {
- public static void main(String[] args)
- {
- //scanner and loop for number of cases (always the first line of input)
- Scanner input = new Scanner(System.in);
- int numCases = Integer.parseInt(input.nextLine());
- for(int i = 0; i<numCases; i++)
- {
- //takes in the nth case
- String str = input.nextLine();
- //initalizes and resets the sum of the characters vaules for each case
- int sum = 0;
- 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"
- {
- //each character is multiplied by their weight
- //weight is determined by position of character, first characters weight is 10, second weight = 9... last weight = 2
- int weight = 10 - j;
- int digit = Integer.parseInt(str.substring(j,j+1));
- int product = weight*digit;
- sum += product;
- }
- //determines the difference between the sum of the series and the nearest multiple of 11
- int diff = sum%11;
- int checkDigit = 11-diff;
- if ((str.substring(9).equals("X") && checkDigit == 10)||(Integer.parseInt(str.substring(9)) == checkDigit))
- {
- System.out.println("VALID");
- }
- else
- {
- System.out.println("INVALID");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement