This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loops" Watch "Loops" New topic
Author

Loops

Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
I was just wondering, how would you write a for loop to decode a String by trying every possible combination of numbers/letters?
eg.
Something like that I guess. I'm still trying to figure out this problem. Can anybody help me or give me any hints? Am I doing this totally wrong?
Thanks
-Chris
[ April 03, 2002: Message edited by: Chris Pocock ]

<i>I'm currently reading 'How to program Java' by Deitel</i><br /><b>I am still learning, so be kind. I also know C++, by the way</b>
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
There were a couple of cool algorithms posted yesterday by Brent Worden for performing combinations and permutations (implemented in C++ too).
They could be well worth a look.
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=33&t=005058
James.
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
The algorithm from that link that was in that guys post didn't look like it would help me out... I edited my original post to make the code better (I was in a rush before).
I'm still (very) open to any help .
Thanks heaps
-Chris
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
I thought those algorithms were almost spot on for what you're trying to do (brute force approach).
If you look at a definition of what a permutation is (it's been 10 or so years so I did Mathematics so I had to look it up):
http://mathforum.org/dr.math/faq/faq.comb.perm.html
Those algorithm's will return you the possible values in a permutation (given the right input).
Ok, as I said my math is a little out but Ill give it a shot anyway.
Lets say you have a "password" of length 5, that can be any combination of "lowercase letters" and numbers (ie. a..z 0..9)
So the number of possible permutations of that password is:
36_P_5 = 36! / (36 - 5)! = 45,239,040
My understanding that those algorithms posted would actually return in each iteration a particular set of values.
In the above case there would be ~45 Million iterations.
Please feel free to flame if my Math is incorrect.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Ok I had to try it for myself:

You should get output:
Your password was js1, guessed after 11314 tries
[ April 04, 2002: Message edited by: james swan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loops
 
Similar Threads
Using final as program-wide variables
Switch statement and constant variables
Compile time constant ?
Symbol can't be found
final variable lives on?