Skip to content

Instantly share code, notes, and snippets.

@jononon
Created September 29, 2015 04:20
Show Gist options
  • Save jononon/a9452232c2ef081553a9 to your computer and use it in GitHub Desktop.
Save jononon/a9452232c2ef081553a9 to your computer and use it in GitHub Desktop.
public class dogtagexample{
/**
* Checks that a dog tag is valid
* @param dogTag the dog tag to be checked
* @return true if the dog tag is a valid dog tag
*/
public static boolean checkDogTag (String dogTag) {
if(dogTag.length()!=4)
throw new IndexOutOfBoundsException();
int sumOfElements = 0;
for(int i = 0; i <= 2 ; i++)
sumOfElements+=Integer.parseInt(dogTag.substring(i,i+1),10);
char dogID = (char) (70+sumOfElements%10);
if(dogTag.equals(dogTag.substring(0,3)+dogID))
return(true);
else
return(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment