We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
splitTokens() (Read 773 times)
splitTokens()
Nov 2nd, 2009, 9:11am
 
hi all,
i write a simple program that it will first import a .txt file the content of the .txt file is like that:

apple
hill
nurse
you
win

every words followed by a line feed (\n).then the program will separate each word by splitTokens() and assign each word to an array element:


String ref2[] = loadStrings("electriccode.txt");
String str2 = ref2[0];
String[] ref = splitTokens(str2);

size(400,400);
background(0);

println(ref);


From processing.org reference, it said that "If no tokens character is specified, any whitespace character is used to split. Whitespace characters include tab (\t), line feed (\n), carriage return (\r), form feed (\f), and space."i try to use this String[] ref = splitTokens(str2); to split line feed and assign the words to array ref[] like this:

ref[0] = "apple";
ref[1] = "hill";
ref[2] = "nurse";

but i failed, can anyone tell me why i cannot use splitTokens() to split line feed?thanks.
Re: splitTokens()
Reply #1 - Nov 2nd, 2009, 9:28am
 
If you look at the contents in the returned array from loadStrings() you'll see it has already split the contents of the text file by line so there's no need to split it further.

Try:

Code:
for (int i=0; i < ref2.length; i++) {
   println(ref2[i]);
 }


and you should see what I mean.
Re: splitTokens()
Reply #2 - Nov 2nd, 2009, 9:29am
 
there is no need to split it.
When using loadString, every line is loaded

try this :

String ref[] = loadStrings("nouns.txt");


size(400,400);
background(0);

println(ref);
Page Index Toggle Pages: 1