aspose file tools*
The moose likes Beginning Java and the fly likes Searching a char[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching a char[] " Watch "Searching a char[] " New topic
Author

Searching a char[]

Pan Niko
Ranch Hand

Joined: Mar 19, 2011
Posts: 86

Hello,

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

AN: Alphanumeric
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...

thank you
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
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.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

So, what exactly do you want to do; check if a string has the correct format? A regular expression would be the perfect tool for that. For example:

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Pan Niko
Ranch Hand

Joined: Mar 19, 2011
Posts: 86

Thank you both for your answers. Jesper de Jong answer looks great, but for your answer Kurt Van Etten how i will check the structure, i will do it one by one character? Because that is not so efficiency.

Thank you

EDIT: My mistake didt spot the last line in Jesper de Jong post
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
Hi Wouter, I had actually at first written in my response that Pan might want to look at the Character isDigit(), isLetter(), and isLetterOrDigit() methods. I changed what I wrote because it looked like the problem was specifically requiring ASCII character ranges. It's certainly worth being aware of this, though, for situations where you want to allow for correct internationalization.

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.)
 
Don't get me started about those stupid light bulbs.
 
subject: Searching a char[]