Results 1 to 13 of 13

Thread: Problem in code

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

    Default Problem in code

    The for loop(with int = p) and the if code(encryptFormat[i].equals (encryptFormat[p])) does not do its job. This loop and if code is suppose to check so the same string is not entered twice(i.e no duplicate, all strings in this array should be different). It should work, I dont get it. What am I doing wrong?

    PHP Code:
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class EncryptionCodeCreator
    {
    
    	public static void main (String[] args) throws Exception
    	{
    
    		StringBuilder[] encryptFormat = new StringBuilder[1000];
    		char[] encryptChar = {'@', '*'};
    
    		for (int f = 0; f < encryptFormat.length; f++)
    			encryptFormat[f] = new StringBuilder ("|aaaaaaaaaa|");
    
    		FLoop:
    		for (int i = 0; i < encryptFormat.length; i++)
    		{
    			for (int j = 1; j < 11; j++)
    				encryptFormat[i].setCharAt (j, encryptChar[(int) (2 * Math.random())]);
    
    			for (int p = 0; p < i; p++)
    			{
    				if (encryptFormat[i].equals (encryptFormat[p]))
    				{
    					i--;
    					continue FLoop;
    				}
    			}
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    What am I doing wrong?
    Try debugging your code by adding a println before the if statement that shows the values of all the variables being tested. The output should show you why your code is not working.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, if you don't want duplicates, consider using a set.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    encryptFormat is an array of StringBuilder. You then call the equals method. Since StringBuilder does not have an equals method it inherits it from Object. Thus it does not compare the contents of the StringBuilder. If you want to see if the Strings in the StringBuilders are the same you need to extract the Strings and compare them.

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

    Default

    nice find Junky. I fixed the problem now. But a new problem have arrived. If I set encryptFormats length to higher than 1024, program will f*ck up. Int i will never be higher than 1024, I have waited over 10 minutes, it got stuck for some reson. I added debug codes and I saw that variable i never passed 1024. it seems this problem is beyond my control. Is there some kind of limit?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Have you done the math to see if there are more than 1024 different patterns?
    Once you gen all the possibilities you will never find another new one.
    What is 2^10?

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

    Default

    Oh, my calculation was wrong(I thought there could be 100 million different patterns). I suck at math
    Anyway, fixed my problem.
    I am impressed by the java speed, I can produce 10000 different patterns in just a few seconds.

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

    Default

    Unrelated question(to you who know good math).
    My pattern have 3 types of characters now(@, * and $), and the length of each pattern is 10 characters. How do I calculate the max number of different patterns?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    If each position can have 3 different values, how many unique values are there in 10 positions?
    Or a related question:
    If each position can have 10 different values, how many unique values are there in 3 positions?
    10*10*10 or 10^3 or 1000

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

    Default

    Thanks.
    New problem, after an array enter a loop, the elements become very long. I have added debugs before and after this loop, before the loop, its good, but after, it get f*cked.

    Java Code:
    arraystuff.printarray  (theTextFile); [B]//Here is it good.....[/B]
    		for (int p = 0; p < theTextFile.length; p++)
    		{
    			theTextFile[p] = theTextFile[p].replace (" ", theKey[0][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("A", theKey[1][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("B", theKey[2][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("C", theKey[3][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("D", theKey[4][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("E", theKey[5][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("F", theKey[6][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("G", theKey[7][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("H", theKey[8][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("I", theKey[9][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("J", theKey[10][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("K", theKey[11][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("L", theKey[12][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("M", theKey[13][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("N", theKey[14][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("O", theKey[15][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("P", theKey[16][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("Q", theKey[17][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("R", theKey[18][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("S", theKey[19][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("T", theKey[20][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("U", theKey[21][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("V", theKey[22][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("W", theKey[23][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("X", theKey[24][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("Y", theKey[25][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("Z", theKey[26][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[27][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[28][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[29][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("a", theKey[30][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("b", theKey[31][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("c", theKey[32][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("d", theKey[33][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("e", theKey[34][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("f", theKey[35][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("g", theKey[36][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("h", theKey[37][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("i", theKey[38][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("j", theKey[39][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("k", theKey[40][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("l", theKey[41][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("m", theKey[42][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("n", theKey[43][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("o", theKey[44][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("p", theKey[45][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("q", theKey[46][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("r", theKey[47][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("s", theKey[48][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("t", theKey[49][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("u", theKey[50][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("v", theKey[51][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("w", theKey[52][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("x", theKey[53][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("y", theKey[54][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("z", theKey[55][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[56][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[57][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[58][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("0", theKey[59][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("1", theKey[60][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("2", theKey[61][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("3", theKey[62][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("4", theKey[63][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("5", theKey[64][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("6", theKey[65][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("7", theKey[66][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("8", theKey[67][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("9", theKey[68][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("!", theKey[69][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("\"", theKey[70][(int) (codesPerChar * Math.random())]);//The " character
    			theTextFile[p] = theTextFile[p].replace ("#", theKey[71][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[72][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("%", theKey[73][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("&", theKey[74][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("/", theKey[75][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("(", theKey[76][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (")", theKey[77][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("=", theKey[78][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("?", theKey[79][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("`", theKey[80][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("^", theKey[81][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("*", theKey[82][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("_", theKey[83][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (":", theKey[84][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (";", theKey[85][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (">", theKey[86][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("<", theKey[87][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (",", theKey[88][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace (".", theKey[89][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("-", theKey[90][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("'", theKey[91][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[92][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[93][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("+", theKey[94][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[95][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[96][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("@", theKey[97][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("", theKey[98][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("$", theKey[99][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("{", theKey[100][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("[", theKey[101][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("]", theKey[102][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("}", theKey[103][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("\\", theKey[104][(int) (codesPerChar * Math.random())]);//The \ character
    			theTextFile[p] = theTextFile[p].replace ("~", theKey[105][(int) (codesPerChar * Math.random())]);
    			theTextFile[p] = theTextFile[p].replace ("|", theKey[106][(int) (codesPerChar * Math.random())]);
    		}
    arraystuff.printarray  (theTextFile);[B]//but not here.[/B]
    More detail:
    First, the array look like this:
    Java Code:
    a
    ab
    abc
    abcd
    After the loop, it looks like this:
    |**$*$@*@**||**$*$@*@**|*@*|**$*$@*@**|*@*$**@@*@| **$*$@*@**||**$*$@*@**|*@*$**@ - Pastebin.com

    What I want, and what it is suppose to do, is to look like this after the loop:
    Example(the codes are random):
    Java Code:
    |@*@*$**@*@| //This represent "a"
    |@**@@*$*@@||$**@$$@$@*| //This represent "ab"
    |*$$$@$$$$$||**$*****$$||*$*@@@****| //This represent "abc"
    |$$$*$$@$*$||$*$@@$$**$||*@$*$$$@@@||$*@*$@*$$*| //This represent "abcd"
    Last edited by Pojahn_M; 06-06-2011 at 01:12 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    What I want, and what it is suppose to do, is to look like this after the loop:
    Add a "\n" in where you want the following data to go on a new line.

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

    Default

    where the program creates a new line is not a problem. The problem is that the array is like 20 kilo byte large after the loop(just a few byte large before the loop). That doesn't make sense to me.
    I have carefully checked how replace method works, and this should not happen.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Shorten the number of loop iterations and printout what is going on in the loop to see where the problem is.

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  2. Replies: 9
    Last Post: 09-21-2010, 05:15 PM
  3. Help me!!!!!!!! Problem with Code!!
    By Miyaki in forum New To Java
    Replies: 0
    Last Post: 03-08-2009, 01:26 AM
  4. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 09:34 AM
  5. Problem with my first code
    By paul in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 05:09 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
  •