Question: I have a java problem. I am trying to read a txt file which has a variable number of integers per line, and for each line I need to sum every second integer! I am using scanner to read integers, but can't work out when a line is done. Can anyone help pls?

Answer: 
have a look at the [BufferedReader](http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html) class for reading a textfile and at the [StringTokenizer](http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html) class for splitting each line into strings.

```
String input;
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
while ((input = br.readLine()) != null) {
  input = input.trim();
  StringTokenizer str = new StringTokenizer(input);
  String text = str.nextToken(); //get your integers from this string
}

```
If I were you, I'd probably use [FileUtils](http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html) class from [Apache Commons IO](http://commons.apache.org/io/). The method [`readLines(File file)`](http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readLines%28java.io.File%29) returns a List of Strings, one for each line. Then you can simply handle one line at a time.

Something like this: 

```
    File file = new File("test.txt");
    List<String> lines = FileUtils.readLines(file);
    for (String line : lines) {
        // handle one line
    }

```

(Unfortunately Commons IO doesn't support generics, so the there would be an unchecked assignment warning when assigning to List<String>. To remedy that use either @SuppressWarnings, or just an untyped List and casting to Strings.)

This is, perhaps, an example of a situation where one can apply "[know and use the libraries](https://stackoverflow.com/questions/822768/what-are-the-pitfalls-of-a-java-noob/826344#826344)" and skip writing some lower-level boilerplate code altogether.