We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello ladies and gents,
I am trying to read a text file and so far I have got this....
String[] dna;
int index = 0;
void setup() {
size(500, 500);
background(0);
stroke(255);
frameRate(12);
dna = loadStrings("dna.txt");
}
But the actual text file contains 4 random letters scrambled up, such as "cccgtacgag"
I am trying to figure out read out specific letter and translate them into a certain number.
Does anyone have any clue on how to do this? Any help would be appreciated
Cheers,
Answers
You could split the strings in characters, and then substitute each character with a certain number - if I understood correctly (I guess you want a specific number and not the ASCII value?). So you could pass it into the following function:
Hey thanks so much for the reply. I really new at this so I'm not exactly sure when to put all of this in my code because it keeps saying cannot find 'content'
Also just before your reply, I found a function called byte, could that also be used?
Characters in Java are not bytes, they are of type
char
.The "content" is a variable name that actually should be replaced with your dna variable. This function I posted takes a large string i.e. your text file, and creates a char array (separate characters from strings). Then it performs the check to replace whatever you want. The result should return to your string variable dna (or content in my example). If you print the string after the function you will see what I mean. If you are new at this, have a look at this tut: http://processing.org/tutorials/text/
Ok i gotcha, yea I've read and re-read that page and slowly getting it. I'm having a hard time knowing in where to put the code you sent me. (If I should put it above void setup, or in it)
.
Is this possible?
You should take a reading at Processing's reference about the Array structure:
http://processing.org/reference/Array.html
For there's no method called length() for it. Rather, we gotta use its field length! 8-X
Didn't pay attention data was a String already! b-(
Hmm... Just to make sure -> Does your "dna.txt" file consist of solely of 1 line?
Be aware that if it happens to exist more lines than just
lines[0]
within that file, you're ignoring and discarding them all! (~~)Another observation is about those 4 variables -> a, c, g, t.
I can't figure out what they're for besides being assigned values 2, 3, 4, 5 respectively!? :-<
I did some changing up and I have instead tried to make it so each letter makes a different sound, although I cannot get it to keep looping. It will make the first sound then will stop and not play the other letters, any ideas?
Concerning the code I posted you will have to put the String editFileContent function by its own - outside setup or draw. So when you call the following line from setup, it will run the function and return the result for post-processing.
content = editFileContent( content.toLowerCase() );
In the code you posted, I find odd that you create an index that constantly adds to itself, you will get an error soon. Why not try to use a while or for loop to define your index count? Also, bear in mind that when you read your text file contents it only happens once in the setup function.
To have a loop of sounds based on your letters that are inside your text, why not try to use a delay between them? Use the println function to see what is coming out from your if statements and what letter is being validated as true. Also do the following - if you are not sure if the letter is capitalized, or use the content.toLowerCase() as in my example (where content is the String)
if(letter == 'G' || letter == 'g' ){