Im building a Java programm, that takes an ID. This ID (String) must be 16 characters long and with specific structure:
AN AN AN AN AN N N N N N N AN AN AN A A
N: Number (0-9)
A: Alphabetic (A-Z)
Until now im converting the String to charArray, and create another char array that contains letters A-Z but i dont know how to check if characters from 0-4 is letters or numbers etc...
As Kurt says you can get individual characters out of a string using the charAt() method. You can get parts out of the string by using the substring() method. For example, to get a string containing the first alphanumeric part of 5 characters, you could do id.substring(0, 5).
Kurt Van Etten wrote:You don't really need to convert the String to a char array, since the String charAt() method allows you to look at the individual characters one by one. In any event, there is a standard programming trick for doing this type of thing: the numeric values for the digits '0' through '9' appear in order in the ASCII sequence, and likewise for the alpha characters 'A' through 'Z'. Since chars can be used like numbers in expressions, this allows you to use <=, =>, etc. in your code to determine if the chars in the ID meet your criteria.
The problem with that "trick" is that it doesn't work well. That is for non-latin characters. For instance:
Pan, you're right that Jesper's approach is better than mine, both in terms of efficiency and conciseness of code, if you're familiar with regular expressions. (Of course, behind the scenes the regular expression matching is still scanning along the input String character by character, but it's likely to be doing it much more efficiently than some routine you've manually coded.)