Java syntax is simular, please assist!
Author |
Message |
TokenHerbz
|
Posted: Sat Dec 24, 2011 9:39 pm Post subject: Java syntax is simular, please assist! |
|
|
My problem is that I cant seem to break up my string into arrays of strings of the integers, being determined by the first number of the string.
This is coded in Processing, an easier formatted type of java, So Thats why i'm posting it here...
code: |
String encoded_message = "642187071124859719066195428706357906";
int amount_of_characters = 0; //length of the array required
String[] characters = new String[0]; //character array
int count_char_length = 0; //how much numbers are used in the character
for (int string_position = 0; string_position <= encoded_message.length(); string_position++) { //-1 because array starts at 0 not 1.
count_char_length = encoded_message.charAt(string_position); //finds how long the character is
amount_of_characters += 1; //we have a new character we add it
characters = expand(characters, amount_of_characters); //increase our array to match how much chars we have
string_position += amount_of_characters; //skip the rest of the numbers, getting the length of the next char to use.
}
println(characters.length); //tests how long the message is
//set each array to blank so we can ass the dycription to the arrays
for (int array_pos = 0; array_pos <= characters.length -1; array_pos++) { //-1 to keep array 0=1.
characters[array_pos] = "";
}
//now we have our encryiption broken up into possible characters,
//we have to cycle threw them adding the length of those characters
//to the respectful character array, 1 array per character...
int char_start_pos; //keep position in our string
int char_end_pos;
amount_of_characters = 0; //reset this to zero, we start from the begining
for (int array_pos = 0; array_pos <= characters.length -1; array_pos++) {
for (int string_position = 0; string_position <= encoded_message.length(); string_position++){
//as we go threw the strings lets isolate how long our character is encoded
count_char_length = encoded_message.charAt(string_position);
//now we need to count from start to finish, being string position + 1 (start of number, to length of the number
//then setting string_position to the one after that to read the length of the next string.
for (int char_pos = (1 + string_position); char_pos <= 1+(string_position + count_char_length); char_pos++) {
characters[array_pos] += encoded_message.charAt(char_pos);
}
string_position += amount_of_characters;
}
}
//now lets test to see if this code holds any values correctly, starting with array[0] should be 421870
println(characters[0]); //broken, doesn't print anything //get errors now too
|
I want to beable to break up the above string by doing the following,
The first number is a 6, represents the next 6 letters as array 1...
then after that is a 7, represents the next 7 letters as array 2...
and so fourth..
the array should maintain the string format not adding the numbers up into a sum....
thanks for any ideas/suggestions, the error im getting now "StringOutOfBounds" however before that, with no errors, it would not show that the arrays had any values added, just a blank "". |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Sat Dec 24, 2011 9:47 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
the heck is going on here?
code: |
for (int string_position = 0; string_position <= encoded_message.length(); string_position++) { //-1 because array starts at 0 not 1.
count_char_length = encoded_message.charAt(string_position); //finds how long the character is
amount_of_characters += 1; //we have a new character we add it
characters = expand(characters, amount_of_characters); //increase our array to match how much chars we have
string_position += amount_of_characters; //skip the rest of the numbers, getting the length of the next char to use.
}
|
Specifically, start printing out string_position at each iteration, and see if it's what you expect it to be. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
TokenHerbz
|
Posted: Sat Dec 24, 2011 9:54 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
Nope its not what i expect it to be at all, sigh..
thanks i'll start with that. |
|
|
|
|
|
TokenHerbz
|
Posted: Sat Dec 24, 2011 10:03 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
my first outputs should be 6/7/7
im constantly getting 6/2/*, i'll have to rethink this... |
|
|
|
|
|
TokenHerbz
|
Posted: Sat Dec 24, 2011 10:16 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
tony if you could give me some logic code to help me out?! I would appreciate that, that is if you understand what im aiming for.. |
|
|
|
|
|
Tony
|
Posted: Sat Dec 24, 2011 10:54 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
Reading things one character at a time would probably be easier to think about. At the very least, it works much better than reading them one at a time and also jumping ahead
Quote:
for (int string_position = 0; string_position <= encoded_message.length(); string_position++) { //-1 because array starts at 0 not 1.
count_char_length = encoded_message.charAt(string_position); //finds how long the character is
amount_of_characters += 1; //we have a new character we add it
characters = expand(characters, amount_of_characters); //increase our array to match how much chars we have
string_position += amount_of_characters; //skip the rest of the numbers, getting the length of the next char to use.
}
That doesn't really work. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
S_Grimm
|
Posted: Sat Dec 24, 2011 11:27 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
if i understand this right, the string at array[0] should be the first 6 letters (numbers) to after the first digit. Then array[1] should be the next 7 characters after 7. Now, starting at the 7 that occurs, you are at element 7 in string[x]? would you not be able to substring that starting at element X and ending at element X+Y where Y = the number before the X? also what if the third iteration (being 7) makes attempts to access an element out the bounds of the length of the string? I realize thats not happening here, but???
ie
code: |
for (int i = 0; i < current_position + X; ++i )
{
array[currentElement] += string[i];
}
|
sorry if im not making sense, i think i have an idea of what your trying to do. ill type something else out monday or tuesday when im fully awake. |
|
|
|
|
|
crossley7
|
Posted: Sun Dec 25, 2011 10:43 am Post subject: RE:Java syntax is simular, please assist! |
|
|
you could have a similar type of code, except change the for to be
code: | while (string_position < encoded_message.length())
{
....
} |
This would require declaring the string position variable beforehand, but should identify the characters you want |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Sun Dec 25, 2011 12:38 pm Post subject: RE:Java syntax is simular, please assist! |
|
|
I have reworked this code however im getting an out of range error with my string, which i wrote this out and it should work.
the place in the string + the length of the character should not exceed the length of the string even at the end.
code: |
String encoded_message = "642187071124859719066195428706357906";
int array_size = 1; //size of the array
String[] characters = new String[array_size];
int char_start_pos = 0;
int char_length = 0;
for (int message_pos = 1; message_pos <= encoded_message.length(); message_pos++) {
characters = expand (characters, array_size);
characters[array_size -1] = "";
char_length = encoded_message.charAt(message_pos);
char_start_pos = message_pos + 1;
for (int char_pos = 0; char_pos <= char_length -1; char_pos++) {
characters[array_size-1] += encoded_message.charAt(char_start_pos + char_pos);
}
message_pos += char_length;
array_size += 1;
}
|
my logic is this,
6 = amount of string in the char
421870 should be assigned to the first array
6 position = 1
+ length of char = 6
totals 7, which is next char.
cycle
adding the values to the array is like this
starting position = 2 (being the 4).
counts 0 .. length (6) - 1, being 6 positions but letting me go like starting position (2) + counting max (5) = 7 .. which is last number to add the 0.
6421870
1234567
if you understand that?! Would like some insight... thanks! |
|
|
|
|
|
TokenHerbz
|
Posted: Thu Dec 29, 2011 1:08 am Post subject: RE:Java syntax is simular, please assist! |
|
|
Solved it, if your interested heres the code:
code: |
String encoded_message = "6421870711248597190661954287063579067112485971601608711248597133099561038185428707140492371124859716016087160160871906619542870675356661105877140492369999957164302754665154287064389715428707122503871481539715208707
11248595428707190661971481539717279955428706999995716016087112485954287071225038699999571771556712597077144289271191011542870699999554287071191011714815397148153971092722542870710302967160160871124859699999571330995
61176445428706357906714815397125970771442892711910115428707136762671481539716851547164302754287071481539711576205428706456528699999571771556699999554287071520870716016086999995710612037168515471259707710612037112485
95428706438971542870699999571643027716430277172799571404923711248596287491542870672899571225038699999571685154574083716430275428707168515471225038699999571685154628749154287067786837148153971727995542870712250386999
99571771556711248597144289257408371685154628749154665162874915428706778683714815397172799557408371771556711248595428707103029671124859711248597144289254287071191011711248597168515471685154712597077144289271191011542
87071092722716016087172799571442892713309955428706999995714428927109272254287071225038712597077168515471685154712597077144289271191011542870714815397144289254287071225038714815397168515454287071030296699999571030296
71124859716430276287491546651628749154287064052197148153971481539710927225428707168515471481539542870712250387112485969999957160160854665154287064389715740837136762671367626542870716430277112485971124859542870719066
19714815397172799554287071259707714428925428707168515471225038711248595428707144289271124859718158435428707190661971124859699999571601608611764454287063429957122503871124859711248597160160871643027546651";
int array_size = 0;
int char_length = 0;
for (int message_pos = 0; message_pos <= encoded_message.length() -1; message_pos++) {
array_size += 1;
char_length = int(Character.toString(encoded_message.charAt(message_pos)));
message_pos += char_length;
}
String[] characters = new String[array_size];
char_length = 0;
for (int array_pos = 0; array_pos <= characters.length; array_pos++) {
characters[array_pos] = "";
for (int message_pos = 0; message_pos <= encoded_message.length() -1; message_pos++) {
char_length = int(Character.toString(encoded_message.charAt(message_pos)));
for (int char_pos = 1; char_pos <= char_length; char_pos++) {
if ((message_pos + char_pos) <= (encoded_message.length() - 1)) {
characters[array_pos] += Character.toString(encoded_message.charAt(message_pos + char_pos));
}
}
message_pos += char_length;
array_pos += 1;
if (array_pos <= characters.length - 1) {
characters[array_pos] = "";
}
}
}
for (int array_pos = 0; array_pos <= characters.length -1; array_pos += 1) {
characters[array_pos] = str(int(characters[array_pos]) + 5);
characters[array_pos] = str(int(pow(int(characters[array_pos]),.33333333333)));
characters[array_pos] = str(int(characters[array_pos]) - 3);
int temp = int(characters[array_pos]);
characters[array_pos] = str(char(temp));
print(characters[array_pos]);
}
|
The decoded message is as follows:
"Hey Derek, merry X-mas! I hope you are having a good break. Doing lots of Java practice I assume? What's that? You haven't?!? You've been getting drunk and hitting on hot babes?!? Good to hear! I'll see you in the new year. Cheers!" |
|
|
|
|
|
|
|