-1

I know the way to read the next and previous lines while reading a file line by line is like

String prev = null;
String curr = null;
String next = null;

Scanner sc = new Scanner(new File("thefile.txt"));

while(sc.hasNextLine()) {
    prev = curr;
    curr = next;
    next = sc.nextLine();

But during the first iteration I get a null pointer exception as I have to work with the current element.

Whats the workaround for this?

    Scanner sc = new Scanner(file);

    String curr = null ;
    String next = null ;


      while (sc.hasNextLine()) {

            curr = next;
            next =  sc.nextLine();
            if(curr.length() > 0 && !curr.equals(". O O")) {
                lines+= curr.substring(0, curr.indexOf(" ")) + " ";

                for(String pat : patStrings)
                {
                // System.out.println(pat) ;
                    Pattern minExp = Pattern.compile (pat);
                    Matcher minM = minExp.matcher(curr);
                    while(minM.find())
                    {
                        String nerType = minM.group(2);
                        if(nerType.contains("B-NE_ORG")){
                            if (next.contains("I-NE_ORG"))
                                orgName+= minM.group(1)+" ";
serendipity
  • 852
  • 13
  • 32
  • 1
    At the beginning, curr and next are both "null". In the first iteration, you assign "curr = next", so curr is still "null". Then "curr.length()" throws the nullpointer exception... – Dominik Vincenz Feb 17 '17 at 06:51
  • http://stackoverflow.com/questions/3983175/1-scanning-current-as-opposed-to-next-line-location-2-scanning-line-x-java – lavina Feb 17 '17 at 06:56

3 Answers3

1

Do

  next =  sc.hasNextLine() ? sc.nextLine() : null;
  while (sc.hasNextLine()) {
        curr = next;
        next =  sc.nextLine();

By the time you enter the while loop and if the file has two or more lines, the processing can start as per your code.

James Jithin
  • 10,183
  • 5
  • 36
  • 51
1

You most likely need to read two lines first to get your variables setup properly

String prev = null;
String curr = null;

Scanner sc = new Scanner(new File("thefile.csv"));

if (sc.hasNextLine()) {
    curr = sc.nextLine();
}

while (sc.hasNextLine()) {
    prev = curr; 
    curr = sc.nextLine();
}

If you do need three lines at a time, then you should actually read three lines, then process them.

StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
    sb.setLength(0); // clear the stringbuilder

    String line = sc.nextLine(); 
    if (line.isEmpty()) continue; // if you want to skip blank lines
    else sb.append(line).append("\n");

    for (int i = 1; i < 3 && sc.hasNextLine(); i++) {
        line = sc.nextLine(); 
        sb.append(line).append("\n");
    }
    String[] lines = sb.toString().trim().split("\n");
    if (lines.length == 3) {
        String prev2 = lines[0];
        String prev1 = lines[1];
        String curr = lines[2];

        doStringThings(prev2, prev1, curr);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks! I could make do with two lines as I need to check the current and next elements. In your code you have current and previous. I can just swap them for current and next right? – serendipity Feb 17 '17 at 07:11
  • 1
    You have to read an additional line to get the previous, current, **and next**. I think my second solution would be better. Though you need to perform a length check on `lines` – OneCricketeer Feb 17 '17 at 07:15
  • Using the 2nd version of your code,made tiny modification in the way you declare the string builder. But using this my file gets read from 3rd line onwards instead of 1st. while (sc.hasNextLine()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3 && sc.hasNextLine(); i++) { if(sc.nextLine().length() > 0 && sc.nextLine() != null) sb.append(sc.nextLine()).append("\n"); } String[] elements = sb.toString().split("\n"); prev = elements[0]; curr = elements[1]; next = elements[2]; – serendipity Feb 17 '17 at 07:36
  • `sc.nextLine()` can't be null because of the if statements. And I don't think it can be empty? At least your CSV should never have empty lines if it truly is a CSV – OneCricketeer Feb 17 '17 at 07:38
  • I'm not reading a csv file. Its a .txt of sorts which does have blank lines in between. – serendipity Feb 17 '17 at 07:45
  • I guess I am confused why your question contains `new File("thefile.csv")`, then – OneCricketeer Feb 17 '17 at 07:45
  • Sorry about that! My bad. That was just an example. – serendipity Feb 17 '17 at 07:46
0

I am not pretty sure if this helps but exchange the position of curr = next; and next = sc.nextLine(); Your solution:

curr = next;
next =  sc.nextLine();

My suggestion:

next =  sc.nextLine();
curr = next;
jason
  • 45
  • 6