Results 1 to 13 of 13
Thread: Problem in code
- 06-03-2011, 03:11 AM #1
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; } } } } }
- 06-03-2011, 03:23 AM #2
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.What am I doing wrong?
- 06-03-2011, 04:38 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, if you don't want duplicates, consider using a set.
- 06-03-2011, 04:45 AM #4
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.
- 06-03-2011, 03:27 PM #5
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?
- 06-03-2011, 03:44 PM #6
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?
- 06-03-2011, 04:29 PM #7
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.
- 06-03-2011, 04:31 PM #8
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?
- 06-03-2011, 04:34 PM #9
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
- 06-05-2011, 03:23 AM #10
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.
More detail: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]
First, the array look like this:
After the loop, it looks like this:Java Code:a ab abc abcd
|**$*$@*@**||**$*$@*@**|*@*|**$*$@*@**|*@*$**@@*@| **$*$@*@**||**$*$@*@**|*@*$**@ - 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 12:12 AM.
- 06-05-2011, 03:42 AM #11
Add a "\n" in where you want the following data to go on a new line.What I want, and what it is suppose to do, is to look like this after the loop:
- 06-06-2011, 12:09 AM #12
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.
- 06-06-2011, 12:12 AM #13
Similar Threads
-
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Can anyone see the problem with my code? problem understanding switch (newbish)
By keith in forum New To JavaReplies: 9Last Post: 09-21-2010, 04:15 PM -
Help me!!!!!!!! Problem with Code!!
By Miyaki in forum New To JavaReplies: 0Last Post: 03-08-2009, 12:26 AM -
Problem with code
By jvasilj1 in forum New To JavaReplies: 5Last Post: 02-02-2008, 08:34 AM -
Problem with my first code
By paul in forum New To JavaReplies: 2Last Post: 07-26-2007, 04:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks