Home > Enterprise >  how do i detect a double line break in a data file
how do i detect a double line break in a data file

Time:12-04

I have a data file that consists of a calorie count. the calorie count it separated by each elf that owns it and how many calories are in each fruit. so this represents 3 elves

4323
4004
4070
1780
5899
1912
2796
5743
3008
1703
4870
5048
2485
1204

30180

33734
19662

all the numbers next to each other are the same elf. the separated ones are seperate.

i tried to detect the double line break like so

import java.util.*;
import java.io.*;
public class Main 
{
  public static void main(String [] args) throws FileNotFoundException
  {
    int[] elf = new int[100000];
    int cnt = 0;
    Scanner input = new Scanner(new File("Elf.dat"));
    
    while(input.hasNext())
    {
      elf[cnt]  = input.nextInt();
      if (input.next().equals("\n\n"));
      {
        cnt  ;
      }
    }
    int big = elf[0];
    for (int lcv = 0; lcv < elf.length; lcv  )
      {
        if (big < elf[lcv])
        {
          big = elf[lcv];
        }
      }
    System.out.println(big);
  }
}

I'm trying this to detect the double line break

if (input.next().equals("\n\n"));

but its giving me errors. how would i detect it

CodePudding user response:

Here is another alternative way to do this sort of thing. read comments in code:

public static void main(String[] args) throws FileNotFoundException {
    List<Integer> elfSums;  // Can grow dynamically whereas an Array can not.
    int sum;
    // 'Try With Resources' used here to auto close the reader and free resources.
    try (Scanner input = new Scanner(new File("Elf.dat"))) {
        elfSums = new ArrayList<>(); 
        String line;
        sum = 0;
        while (input.hasNextLine()) {
            line =  input.nextLine();
            if (line.trim().isEmpty()) {
                elfSums.add(sum);
                sum = 0;  // Reset sum to 0 (new elf comming up)
            }
            // Does the line contain a string representation of a integer numerical value?
            if (line.matches("\\d ")) {
                // Yes...add to current sum value.
                sum  = Integer.parseInt(line);
            }
        }
    }
    if (sum > 0) {
        elfSums.add(sum);
    }
    
    // Convert List to int[] Array (There are shorter ways to do this)
    int[] elf = new int[elfSums.size()];
    for (int i = 0; i < elfSums.size(); i  ) {
        elf[i] = elfSums.get(i);
        // For the heck of it, display the total sum for this current Elf
        System.out.println("Elf #"   (i 1)   " Sum: -> "   elf[i]);
    }
    
    /* The elf[] int array now holds the data you need WITHOUT 
       all those empty elements with the array.        */
    
}

CodePudding user response:

Welcome to Advent of Code 22.

As a good rule, never mix nextXXX methods with any other next.

To break up the Blocks you have 2 good options:

  1. Read line by line and fill a new list when you encounter a empty/blank line
  2. Read the whole text fully, then split by the \n\n Combination
  • Related