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.
IndexProgramming Questions & HelpSyntax Questions › reading second word of a line in a txt file
Page Index Toggle Pages: 1
reading second word of a line in a txt file (Read 485 times)
reading second word of a line in a txt file
Dec 17th, 2006, 1:37am
 
hi, im reading the information of a txt file with loadStrings :
String[] data=loadStrings("data.txt");

how can i do if i only need the second word per line of my txt file?
is there any trick?

many thnax


punchik
Re: reading second word of a line in a txt file
Reply #1 - Dec 17th, 2006, 2:32am
 
Code:

String [] data = loadStrings("data.txt");
for(int i = 0; i < data.length; i ++){
if((int)((float)i / 2) == i / 2){
println(data[i]);
}
}
Re: reading second word of a line in a txt file
Reply #2 - Dec 17th, 2006, 7:07am
 
To read the second word of a line, first split the line into an array of words, and then access the second element in the array.  The first example in the split reference should help: http://processing.org/reference/split_.html

st33d's reply prints every even-numbered line (counting from 0) in your file.  An easier way to check for this is to use the modulo operator, %, like so:

Code:

if (i%2 == 0) {
// i is an even number
}

Page Index Toggle Pages: 1