How to load a single character at a time from words in a .txt file?
in
Programming Questions
•
1 year ago
I can load .txt files using, for example:
String lines[] = loadStrings("text.txt");
No problem. I can easily load complete words, but I don't want to load a complete word, I want to load only the individual characters from a word.
I have a poem I'm trying to read into Processsing, and then convert each word into a number, I'm thinking, to keep it simple: once I have each letter, I'll get its ASCII code, but I can't even figure out how to load only one letter at a time from a word.
I have 48 text files supplied to me, of the same poem with variations from the 16th century, and I would like to compare the variations between the poems.
My plan is to start simple, load two text files, two words each, and then figure out a way to load each letter from each word.
That's where I'm stuck.
This code will load the first two lines of a text file, but I can only get the number of lines, and then complete words, I can't figure out how to break the words into individual letters.
I imagine it's probably very simple, help, please?
Please note that I don't know how to use the charAt() code to read from an imported .txt file, only from a String from within the Sketch, and I really don't want to type in all of the supplied poems, or convert the words to ASCII myself, though I could... sigh. I'd rather learn how to break an imported word into its constituent letters.
char letter;
String elegy = "Come, madam";
void setup() {
size(100, 100);
String lines[] = loadStrings("come_madam.txt");
println("there are " + lines.length + " lines");
// use 'e' for 'elegy' for the integer variable in the for loop
for (int e = 0; e < lines.length; e++) {
String[] words = split(lines[e], ' ');
letter = elegy.charAt(4);
println(lines[e]);
println(words);
println(lines[e].length());
println(letter);
}
}
This code will correctly read in the practice lines from a .txt file named come_madam.txt (Elegy 8 by John Donne)
and show in the console that I am getting line counts and complete words. Now, how to break those words, or how to import single characters? Is there some way to use 'split' that will cut at each character?
Thanks,
Scotrick
String lines[] = loadStrings("text.txt");
No problem. I can easily load complete words, but I don't want to load a complete word, I want to load only the individual characters from a word.
I have a poem I'm trying to read into Processsing, and then convert each word into a number, I'm thinking, to keep it simple: once I have each letter, I'll get its ASCII code, but I can't even figure out how to load only one letter at a time from a word.
I have 48 text files supplied to me, of the same poem with variations from the 16th century, and I would like to compare the variations between the poems.
My plan is to start simple, load two text files, two words each, and then figure out a way to load each letter from each word.
That's where I'm stuck.
This code will load the first two lines of a text file, but I can only get the number of lines, and then complete words, I can't figure out how to break the words into individual letters.
I imagine it's probably very simple, help, please?
Please note that I don't know how to use the charAt() code to read from an imported .txt file, only from a String from within the Sketch, and I really don't want to type in all of the supplied poems, or convert the words to ASCII myself, though I could... sigh. I'd rather learn how to break an imported word into its constituent letters.
char letter;
String elegy = "Come, madam";
void setup() {
size(100, 100);
String lines[] = loadStrings("come_madam.txt");
println("there are " + lines.length + " lines");
// use 'e' for 'elegy' for the integer variable in the for loop
for (int e = 0; e < lines.length; e++) {
String[] words = split(lines[e], ' ');
letter = elegy.charAt(4);
println(lines[e]);
println(words);
println(lines[e].length());
println(letter);
}
}
This code will correctly read in the practice lines from a .txt file named come_madam.txt (Elegy 8 by John Donne)
and show in the console that I am getting line counts and complete words. Now, how to break those words, or how to import single characters? Is there some way to use 'split' that will cut at each character?
Thanks,
Scotrick
1