• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching a char[]

 
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic