I have a large file to read to get the data out of the columns. File size varies, but is usually around 1000 to 3000 lines. What I need to do is pull the data(numbers) from column 3 ONLY if the number in column 4 of that same line is >0.9.
Example: 1826.602 116.001 0.68 0.92
Since column 4 is greater than 0.9, I want to pull and save the value of column 3, 0.68.

Looking around I found the String.split() method, but I've no idea how to make it do what I need it to, or even if it's the right method for the job. Any feedback would be greatly appreciated.

Recommended Answers

All 6 Replies

String.split() would work just fine. You can use a basic structure like so

BufferedReader reader = new BufferedReader(new FileReader("someFile.txt"));
String line = null;
while ( (line=reader.readLine()) != null){
    // Splitting the read line on a single space, 
    // if it's tab delmited, use "\t" instead
    String[] columns = line.split(" ");

    // Check the value of fourth column
    if (Float.valueOf(columns[3]) > 0.9f){
        // do something with third column data, columns[2]
    }
}
reader.close();

(Exception handling, etc. left out for brevity)

This will work perfectly....the only problem now is that I'm getting this error:

The operator > is undefined for the argument type(s) Float, float

Why is there a problem comparing the two Float values?

My bad, you would want to use Float.parseFloat(String) for that particular comparison.

That part is now working good, but I've encountered a snag that I forgot about. Within the file I'm reading, the data is formatted in a specific way:

1849.079    130.211     9.18  0.00
    422.083    131.277     8.64  0.00

It won't let me show it properly, but if you slide the top row to the left one space, you'll see that there is an extra space in front of the second row. This same spacing issue appears in columns 2 and 3 as well. Is there a way I can just ignore the spaces between each column or something to that effect? A little digging has revealed the Scanner and it seems like it may work. Can I use the Scanner to eliminate or ignore whitespace?

You could use Scanner if you wish, but I think splitting on one or more whitespace characters would be easier: split("\\s+")

It's all working now. Thank you much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.