Search code examples
javaapache-commons-io

Scanner to reset pointer at previous line


My problem could be solved if Scanner class had previous() method on it. I am asking this question to know if there are any methods to achieve this functionality.

Input: a file with contents like

a,1
a,2
a,3
b,1
c,1
c,2
c,3
c,4
d,1
d,2
d,3
e,1
f,1

I need to create a list of all lines that has same alphabet.

try {
            Scanner scanner = new Scanner(new File(fileName));
            List<String> procList = null;
            String line =null;
            while (scanner.hasNextLine()){
                line = scanner.nextLine();
                System.out.println(line);
                String[] sParts = line.split(",");
                procList = new ArrayList<String>();
                procList.add(line);
                boolean isSamealpha = true;
                while(isSamealpha){
                    String s1 = scanner.nextLine();
                    if (s1.contains(sParts[0])){
                        procList.add(s1);
                    }else{
                        isSamealpha = false;
                        System.out.println(procList);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

I get output like

a,1
[a,1, a,2, a,3]
c,1
[c,1, c,2, c,3, c,4]
d,2
[d,2, d,3]
f,1
[f,1]

As you can see it missed list for b and e. If I has scanner.previous() method, I would have put it in else of second while loop. Because there is no previous method, I am stuck.

Please let me know if there are any methods I can use. I can't use FileUtils.readLines() because its a 3GB file and I don't want to use my java memory to store all the file.


Solution

  • Below is some sample code that should help. (I only typed this; I did no checking.)

    Scanner scanner = new Scanner(new File(fileName));
    List<String> procList = null;
    String line = null;
    String previousAlpha = null;
    while (scanner.hasNextLine()){
        line = scanner.nextLine();
    
        if (previousAlpha == null) {
            // very first line in the file
            procList = new ArrayList<String>();
            procList.add(line);
            System.out.println(line);
            previousAlpha = line.split(",")[0];
        }
        else if (line.contains(previousAlpha)) {
            // same letter as before
            procList.add(line);
        }
        else {
            // new letter, but not the very first
            // line
            System.out.println(procList);
    
            procList = new ArrayList<String>();
            procList.add(line);
            System.out.println(line);
            previousAlpha = line.split(",")[0];
    
        }
    }