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

    Default Stuck in a infinite loop

    For loops...

    Clean code:

    PHP Code:
    for (int a = 0; a < theFile.length; a++)
    {
    	SecondLoop:
    	for (int b = 0; b < theFile[a].length (); b++)
    	{
    		char cc = (char) theFile[a].charAt (b);
    
    		for (int c = 0; c < encryptChar.length; c++)
    		{
    			if (cc == encryptChar[c])
    				continue;
    
    			theFile[a] = theFile[a].deleteCharAt (b).insert (b, this.key[theChars.length][(int) (codesPerChar * Math.random())]);
    			continue SecondLoop;
    		}
    	}
    }
    Some debugging:

    PHP Code:
    for (int a = 0; a < theFile.length; a++)
    {System.out.println ("Debug1");
    	SecondLoop:
    	for (int b = 0; b < theFile[a].length (); b++)
    	{System.out.println ("Debug2");
    		char cc = (char) theFile[a].charAt (b);
    
    		for (int c = 0; c < encryptChar.length; c++)
    		{System.out.println ("Debug3");
    			if (cc == encryptChar[c])
    				continue;
    			System.out.println ("Debug4");
    			theFile[a] = theFile[a].deleteCharAt (b).insert (b, this.key[theChars.length][(int) (codesPerChar * Math.random())]);
    			continue SecondLoop;
    		}
    	}
    }
    Debugging results:

    Java Code:
    Debug1 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4 
    Debug2 
    Debug3 
    Debug4

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Is there a problem here? Not only that, but this is a Java Forum not a PHP forum.
    My API:
    Java Code:
    cat > a.out || cat > main.class

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

    Default

    Can you explain what you want your code to do when it executes?
    You haven't explained why an infinite loop is not what you want.

    Your debug output needs to show the values of the variables that are controlling the looping. Constant text messages are not going to tell you what you need to know.
    Last edited by Norm; 08-20-2011 at 09:43 PM. Reason: Need better printouts

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

    Default

    Quote Originally Posted by kammce View Post
    Is there a problem here? Not only that, but this is a Java Forum not a PHP forum.
    I know, my code its java. the reson I used [PHP][/PHP] is because there is no bloody [JAVA].[/JAVA]

    Quote Originally Posted by Norm View Post
    Can you explain what you want your code to do when it executes?
    You haven't explained why an infinite loop is not what you want.

    Your debug output needs to show the values of the variables that are controlling the looping. Constant text messages are not going to tell you what you need to know.
    Of course I dont want an infinite loop.

    What this code is suppose to do, is to check a string array(and all its elements) if there are characters that are not in the encryptChar array. So basically, those characters in theFile array that are not listed in encryptChar array will be replaced.

    I changed the debugging, and it seems like the second for loop is causing the trouble.
    a is 0.
    b is several thousand, even if theFile[a]s length is 120. How is that even possible?

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

    Default

    a is 0.
    That means you are NEVER executing the end of the loop where a would be incremented.
    How is that possible?
    How is that even possible?
    See the following line:
    continue SecondLoop;
    What are the values of c and cc?

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

    Default

    Lets start over.
    I solved this problem, but I personally think my algorithm was very unprofessional.

    So what I want is, a method, it looks like this:
    public static boolean makeSureItDoesNotExist (char[] arr, char c)

    This method want to make sure that c is not equal to any of the elements in arr.
    If c have a duplicate in arr, return true, otherwise return false.

    Here is my code:

    Java Code:
    	private static boolean IsInEncryptChar (char[] arr, char c)
    	{
    		boolean[] b1 = new boolean[arr.length];
    		boolean b2 = false;
    
    		for (int i = 0; i < arr.length; i++)
    		{
    			if (arr[i] != c)
    				b1[i] = true;
    		}
    
    		for (int j = 0; j < b1.length; j++)
    		{
    			if (b1[j] == false)
    				return false;
    		}
    		return true;
    	}

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

    Default

    Have you tested it? Does it work? If not, do you have a testing program that shows the problem? Please post it.

    What is the variable b2 used for?
    Why do you have an array of boolean?

    If c have a duplicate in arr
    Why not have a single loop, returning true at the first match.

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

    Default

    its the second time I over complicate a simple task. o_0
    Boolean array? oh god.
    I solved this with a simple code as you recommended.

    Java Code:
    	private static boolean IsInEncryptChar (char c)
    	{
    		for (int i = 0; i < encryptChar.length; i++)
    		{
    			if (encryptChar[i] == c)
    				return true;
    		}
    		return false;
    	}

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

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 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
  •