Results 1 to 6 of 6
Thread: Control loop do not work
- 12-26-2011, 12:41 AM #1
Control loop do not work
I have a method that creates an 2D string array with unique elements(i e no duplicates at all in the array).
The code from line 18 to 29 make sure the newly created string is unique. But it does not do what it is intended to. The end "product" have duplicates.Java Code:public void generateKey (File outputFile) throws IOException { short failSafety = 0; String[][] key = new String[THECHARS.length][CODESPERCHAR]; for (int dim1 = 0; dim1 < key.length; dim1++) { Loop2: for (int dim2 = 0; dim2 < key[dim1].length; dim2++) { if (failSafety > 10000) throw new RuntimeException ("Fail safety"); key[dim1][dim2] = ""; for (int codeLength = 0; codeLength < CODELENGTH; codeLength++) key[dim1][dim2] += Character.toString (ENCRYPTIONCHARACTERS[(int) (Math.random () * ENCRYPTIONCHARACTERS.length)]); for (int con1 = 0; con1 < dim1; con1++) { for (int con2 = 0; con2 < dim2; con2++) { if (key[dim1][dim2].equals (key[con1][con2])) { dim2--; failSafety++; continue Loop2; } } } failSafety = 0; } } this.key = key; controlKey (); saveKey (outputFile); initHashMap (); }
I cant see why, some help?
- 12-26-2011, 12:54 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Control loop do not work
you should use a while(true) loop that checks for the values and using a switch breaks out only if there are no duplicates.
- 12-26-2011, 12:55 AM #3
Re: Control loop do not work
What do you do in the code to make sure that there are no duplicates?
A random number generator can produce duplicates.
You need to keep track of what you have used so far and NOT reuse them.
Can you provide testing code that shows the problem?Last edited by Norm; 12-26-2011 at 01:06 AM.
- 12-26-2011, 01:08 AM #4
Re: Control loop do not work
Use another approach.
Generate all the possible keys from aaaaa to zzzzz and use the random number generator to select them one at a time.
null out the selected values to make sure they are not used more than once.
- 12-26-2011, 01:16 AM #5
Re: Control loop do not work
thanks guys, but this code is a <REMOVED>. I will rewrite it using a HashSet.
Last edited by Norm; 12-26-2011 at 01:53 AM.
- 12-26-2011, 01:17 AM #6
Similar Threads
-
Moving control backwards within loop in java
By jharishabh7 in forum New To JavaReplies: 16Last Post: 10-07-2010, 04:20 PM -
Need help with getting second loop to work
By twcast in forum New To JavaReplies: 3Last Post: 02-08-2010, 02:15 PM -
Why won't this while loop work?
By trueblue in forum New To JavaReplies: 6Last Post: 05-23-2009, 08:10 PM -
Really need help with an assignment... counter control loop...
By maxpower1000sa in forum New To JavaReplies: 7Last Post: 02-21-2009, 10:52 PM -
Why doesn't my loop work?
By d0nmin0 in forum Advanced JavaReplies: 8Last Post: 05-26-2008, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks