File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Letter/Number counter

 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there something exists that will allow me to count from say 0-9 and then from a-z. I know that for hexadecimal I could go from 0-f.

Bascially I am trying to loop through some alphanumeric id and find out which ones are valid. It will take me awhile becasue the id is 12 alphanumeric characters.

Off the top of my head I woudl probably create an array from 0-z and just loop that way. I just wanted to know if there was an easier way or something that may be in the api that I just overlooked.
 
Ryan Smith
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to try something using the ASCII char codes, using the String methods isDigit and isLetter...
 
Ryan Smith
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make those methods in the Character class...sorry!

[Edit:]
Alright, let me clarify:


[ July 31, 2004: Message edited by: Ryan Smith ]
[ July 31, 2004: Message edited by: Ryan Smith ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the characters have to be in a particular order? You might check out the regex stuff.
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helped a little but I am still stuck. I am using ascii char but I am lookling at recursion to try and get done what I need to. Time was a factor, but I guess I got to sit down and pull out some computer science and algorithim skills in order to figure out a soultion.

Take for instance I have a three digit id..

000
001
002
...
009
00A
00B
...
00Z
010
011
...
01Z
020
...
...
0Z0
0Z1
...

And so on.

I want to go from 0-Z
0,1,2...9,A,B....Y,Z
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know the decimal system, surely the dual, as you mentioned hex.
Bases are 10, 2, 16.
Not that well known is the octal system with base 8.

Java knows at least the 26+10= 36 base - either in Integer, or Number or String class.
It's in the javadocs.
isn't it: Integer.parseInt (String, base)?

I don't know whether distinction between lower- and uppercase is suppoerted, which would give a 2*26+10 base.

If you can't find it, ask again, and we will look up the javadocs.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Smith:
Bascially I am trying to loop through some alphanumeric id and find out which ones are valid.

I'm still confused as to what you are trying to accomplish. Are you trying to generate possible valid ids by looping through all possible ids?

Is any id valid that contains alphanumeric chars? i.e. 12d8rit3k901?

Are you trying to assign the ids that have not yet been assigned in numerical order?

Are you trying to rule out ids that contain chars other than 0-9 and a-z?

Do A-Z need to be capitalized or are a-z also acceptable as "valid ids"?
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Is any id valid that contains alphanumeric chars? i.e. 12d8rit3k901? "
Yes.


Basically I want to go through all combinations of alphanumeric characters.
Case does not matter, it can be upper or lower.
 
Nick George
Ranch Hand
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest forgetting about any kind of base 36, and build the strings yourself. Just loop through the ascii. One way that might be kind of cool would be to have however many nested for loops. In the very-most inner one, use the cascading loop variables to build your string. The innermost loop will run through all the way before the next one will, which will go all the way until the next one, etc., thus simulating an advancing number system. eh?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
12 nested loops



zzzZZZzzzZZZ(36) = 4738381338321616895 (10)
(4 738 381 338 321 616 895)


This code will run appromaxly 8000 years on my machine, which is about 2 years old and wasn't the fastest, when new.
An actual machine might need about 2000 years.
According to Moores Law, you could wait for about 10 years for a 4 Thz machine, which could solve it in 2 years.
It would mean the same to wait 11 years and run the application in 1 year - results would be there in 12 years.
Waiting longer wouldn�t make much sense
But I would check in 2010, whether Moores Law is still valid

On the other hand:
If you have a cluster of 36x36 machines of 4 Ghz, it could be solved in about 2 years.

[ August 01, 2004: Message edited by: Stefan Wagner ]
[ August 01, 2004: Message edited by: Stefan Wagner ]
 
Dan Walin
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That calculation is probably pessimistic, meaning that you have to run through every single combination before finding the right one. On average you'd probably have to go through 1/2 of the combinations before hitting it - so maybe 4000 years?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Basically I want to go through all combinations of alphanumeric characters.


This might be one way (needs testing, and plenty of time)

 
Jack Kay
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
On the other hand:
If you have a cluster of 36x36 machines of 4 Ghz, it could be solved in about 2 years.


Somebody drank too much coffee this morning!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael:
MAX_RADIX is set to 36, so my solution can't be modified to handle distinguishing of Upper and Lowercase.

While "case does not matter" sounds like "don't distinguish them", the second part of the sentence "it can be upper or lower" sounds like the opposite.

Now Anthony - does it matter or not? do we have 36 different characters, or 62?
Ah - the post before he said:
I want to go from 0-Z
0,1,2...9,A,B....Y,Z


But Michael, I guess every character may occur multiple times - this would especially be my expection for a password.
And though your solution produces less results when truncated to 36 values, (36*35*34* ... *24) - compared to mine (36^12) - it's slower.

But of course - premature optimization is the root of all evil - so forget about my optimized one, and refactor yours!
[ August 01, 2004: Message edited by: Stefan Wagner ]
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
36 Characters...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I guess every character may occur multiple times - this would especially be my expection for a password.


forgot about that

ah well, back to the drawing boards.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic