10

I have searched similar questions, but none helped.

Consider a file :

hi how are you?
where were you?

I want to do few operations after the end of every line. If I use next() it wont tell me when I have reached the end of the first line.

Also I have seen hasNextLine() but it only tells me if there exists another line or not.

Gary
  • 11,083
  • 14
  • 43
  • 68

4 Answers4

19

Consider using more than one Scanner, one to get each line, and the other to scan through each line after you've received it. The only caveat I must give is that you must be sure to close the inner Scanner after you're done using it. Actually you will need to close all Scanners after you're done using them, but especially the inner Scanners since they can add up and waste resources.

e.g.,

Scanner fileScanner = new Scanner(myFile);
while (fileScanner.hasNextLine()) {
  String line = fileScanner.nextLine();

  Scanner lineScanner = new Scanner(line);
  while (lineScanner.hasNext()) {
    String token = lineScanner.next();
    // do whatever needs to be done with token
  }
  lineScanner.close();
  // you're at the end of the line here. Do what you have to do.
}
fileScanner.close();
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • I was thinking in the same way, but thought might some simple functions will help..cause this is time consuming.Thanks +1 –  Mar 03 '13 at 08:23
  • @yoohoo: I'm not sure how it's time consuming as it's only a few lines of code and it runs quite fast. You're welcome, BTW. – Hovercraft Full Of Eels Mar 03 '13 at 08:25
  • @HovercraftFullOfEels Hehe time consuming i mean w.r.t CPU stuff.If there are a lot of lines, time consumption will be directly proportional.Isn't it? –  Mar 03 '13 at 08:32
  • @yoohoo: no, not at all. The only thing really time consuming in the code above is disk I/O access, and the processing that is done after the read is trivial compared to this. I think that you're worrying about exactly the wrong thing. – Hovercraft Full Of Eels Mar 03 '13 at 16:33
  • overcomplicated, keep it simple, use scanner and hasNextLine and nextLine – vantesllar Oct 04 '18 at 09:24
  • It's separating by space! I need to separate on new line. – vishva vijay Oct 24 '19 at 08:08
  • @vishvavijay: re-look at the code -- it's separating by *both* which were the requirements of the original post – Hovercraft Full Of Eels Oct 24 '19 at 10:33
1

You can scan the text line by line and split each line in tokens using String.split() method. This way you know when one line has ended and also have all the tokens on each line:

Scanner sc = new Scanner(input);
while (sc.hasNextLine()){
    String line = sc.nextLine();
    if (line.isEmpty())
        continue;
    // do whatever processing at the end of each line
    String[] tokens = line.split("\\s");
    for (String token : tokens) {
        if (token.isEmpty())
            continue;
        // do whatever processing for each token
    }
}
niculare
  • 3,521
  • 1
  • 22
  • 36
0

Not sure if this is relevant or too late when i read this. I am relatively new to Java but this seemed to work for me when i encountered a similar problem. I just used a DO-WHILE loop with a End of file specifier denoted by a simple string.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;`enter code here`

public class Main {
    public static void main(String[] args) {
        List<String> name = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        String eof = "";

        do {
            String in = input.nextLine();
            name.add(in);
            eof = input.findInLine("//");
        } while (eof == null);

        System.out.println(name);
     }
}
Sunil
  • 1
0

You can use Scanner and the method you mentioned:

        Scanner scanner = new Scanner(new File("your_file"));
        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            // do your things here
        }
vantesllar
  • 429
  • 4
  • 13