Created
May 22, 2013 10:39
-
-
Save ubergesundheit/5626679 to your computer and use it in GitHub Desktop.
VIN validator/decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VIN { | |
//from http://introcs.cs.princeton.edu/java/31datatype/VIN.java.html | |
public static boolean isVinValid(String vin) { | |
int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 0, 7, 0, 9, | |
2, 3, 4, 5, 6, 7, 8, 9 }; | |
int[] weights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
String s = vin; | |
s = s.replaceAll("-", ""); | |
s = s.replaceAll(" ", ""); | |
s = s.toUpperCase(); | |
if (s.length() != 17) | |
throw new RuntimeException("VIN number must be 17 characters"); | |
int sum = 0; | |
for (int i = 0; i < 17; i++) { | |
char c = s.charAt(i); | |
int value; | |
int weight = weights[i]; | |
// letter | |
if (c >= 'A' && c <= 'Z') { | |
value = values[c - 'A']; | |
if (value == 0) | |
throw new RuntimeException("Illegal character: " + c); | |
} | |
// number | |
else if (c >= '0' && c <= '9') | |
value = c - '0'; | |
// illegal character | |
else | |
throw new RuntimeException("Illegal character: " + c); | |
sum = sum + weight * value; | |
} | |
// check digit | |
sum = sum % 11; | |
char check = s.charAt(8); | |
if (sum == 10 && check == 'X') { | |
System.out.println("Valid"); | |
return true; | |
} else if (sum == transliterate(check)) { | |
System.out.println("Valid"); | |
return true; | |
} else { | |
System.out.println("Invalid"); | |
return false; | |
} | |
} | |
private static int transliterate(char check){ | |
if(check == 'A' || check == 'J'){ | |
return 1; | |
} else if(check == 'B' || check == 'K' || check == 'S'){ | |
return 2; | |
} else if(check == 'C' || check == 'L' || check == 'T'){ | |
return 3; | |
} else if(check == 'D' || check == 'M' || check == 'U'){ | |
return 4; | |
} else if(check == 'E' || check == 'N' || check == 'V'){ | |
return 5; | |
} else if(check == 'F' || check == 'W'){ | |
return 6; | |
} else if(check == 'G' || check == 'P' || check == 'X'){ | |
return 7; | |
} else if(check == 'H' || check == 'Y'){ | |
return 8; | |
} else if(check == 'R' || check == 'Z'){ | |
return 9; | |
} else if(Integer.valueOf(Character.getNumericValue(check)) != null){ //hacky but works | |
return Character.getNumericValue(check); | |
} | |
return -1; | |
} | |
} |
So cool thanks a lot !
Thanks!
This help me a lot. Thanks!
Error: Main method not found in class Vin1, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
how to remove this error @bfriscic @Goldenflamer
@12993994 You need a main class with a main method.
https://www.journaldev.com/12552/public-static-void-main-string-args-java-main-method
Call this code from the main method
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!