Results 1 to 6 of 6
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

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

    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 ();
    	}
    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.
    I cant see why, some help?

  2. #2
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

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

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Control loop do not work

    Yes a Set would make sure there are no duplicates.

Similar Threads

  1. Moving control backwards within loop in java
    By jharishabh7 in forum New To Java
    Replies: 16
    Last Post: 10-07-2010, 04:20 PM
  2. Need help with getting second loop to work
    By twcast in forum New To Java
    Replies: 3
    Last Post: 02-08-2010, 02:15 PM
  3. Why won't this while loop work?
    By trueblue in forum New To Java
    Replies: 6
    Last Post: 05-23-2009, 08:10 PM
  4. Replies: 7
    Last Post: 02-21-2009, 10:52 PM
  5. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •