Task: Text processing_1





Java: import java.io.File;
import java.util.*;
import static java.lang.System.out;

public class TextProcessing1 {

    public static void main(String[] args) throws Exception {
        Locale.setDefault(new Locale("en", "US"));
        Metrics metrics = new Metrics();

        int dataGap = 0;
        String gapBeginDate = null;
        try (Scanner lines = new Scanner(new File("readings.txt"))) {
            while (lines.hasNextLine()) {

                double lineTotal = 0.0;
                int linePairs = 0;
                int lineInvalid = 0;
                String lineDate;

                try (Scanner line = new Scanner(lines.nextLine())) {

                    lineDate = line.next();

                    while (line.hasNext()) {
                        final double value = line.nextDouble();
                        if (line.nextInt() <= 0) {
                            if (dataGap == 0)
                                gapBeginDate = lineDate;
                            dataGap++;
                            lineInvalid++;
                            continue;
                        }
                        lineTotal += value;
                        linePairs++;

                        metrics.addDataGap(dataGap, gapBeginDate, lineDate);
                        dataGap = 0;
                    }
                }
                metrics.addLine(lineTotal, linePairs);
                metrics.lineResult(lineDate, lineInvalid, linePairs, lineTotal);
            }
            metrics.report();
        }
    }

    private static class Metrics {
        private List<String[]> gapDates;
        private int maxDataGap = -1;
        private double total;
        private int pairs;
        private int lineResultCount;

        void addLine(double tot, double prs) {
            total += tot;
            pairs += prs;
        }

        void addDataGap(int gap, String begin, String end) {
            if (gap > 0 && gap >= maxDataGap) {
                if (gap > maxDataGap) {
                    maxDataGap = gap;
                    gapDates = new ArrayList<>();
                }
                gapDates.add(new String[]{begin, end});
            }
        }

        void lineResult(String date, int invalid, int prs, double tot) {
            if (lineResultCount >= 3)
                return;
            out.printf("%10s  out: %2d  in: %2d  tot: %10.3f  avg: %10.3f%n",
                    date, invalid, prs, tot, (prs > 0) ? tot / prs : 0.0);
            lineResultCount++;
        }

        void report() {
            out.printf("%ntotal    = %10.3f%n", total);
            out.printf("readings = %6d%n", pairs);
            out.printf("average  = %010.3f%n", total / pairs);
            out.printf("%nmaximum run(s) of %d invalid measurements: %n",
                    maxDataGap);
            for (String[] dates : gapDates)
                out.printf("begins at %s and ends at %s%n", dates[0], dates[1]);

        }
    }
}






Fortran: Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
      INTEGER Y,M,D		
      INTEGER GOOD(24)		
      REAL*8 V(24),VTOT,T	
      INTEGER NV,N,NB		
      INTEGER I,NREC,HIC	
      INTEGER BI,BN,BBI,BBN	
      CHARACTER*10 BDATE,BBDATE	
      LOGICAL INGOOD		
      INTEGER IN,MSG		
      CHARACTER*666 ACARD	
      IN = 10		
      MSG = 6		
      OPEN (IN,FILE="Readings1.txt", FORM="FORMATTED",	
     1 STATUS ="OLD",ACTION="READ")			
      NB = 0		
      NV = 0		
      VTOT = 0		
      NREC = 0		
      HIC = 0		
      INGOOD = .TRUE.	
      BBN = 0		
Chew into the file.
   10 READ (IN,11,END=100,ERR=666) L,ACARD(1:MIN(L,LEN(ACARD)))	
      NREC = NREC + 1		
   11 FORMAT (Q,A)		
      READ (ACARD,12,END=600,ERR=601) Y,M,D	
   12 FORMAT (I4,2(1X,I2))				
      READ (ACARD(11:L),*,END=600,ERR=601) (V(I),GOOD(I),I = 1,24)	
Calculations. Could use COUNT(array) and SUM(array), but each requires its own pass through the array.
   20 T = 0		
      N = 0		
      DO I = 1,24	
        IF (GOOD(I).GT.0) THEN	
          N = N + 1		
          T = T + V(I)		
          IF (.NOT.INGOOD) THEN	
            INGOOD = .TRUE.	
            IF (BN.GT.BBN) THEN	
              BBN = BN		
              BBI = BI		
              BBDATE = BDATE	
            END IF		
          END IF		
         ELSE		
          IF (INGOOD) THEN	
            INGOOD = .FALSE.	
            BDATE = ACARD(1:10)	
            BI = I		
            BN = 0		
          END IF		
          BN = BN + 1	
        END IF		
      END DO		
Commentary for the day's data..
      IF (N.LE.0) THEN	
        WRITE (MSG,21) NREC,ACARD(1:10)	
   21   FORMAT ("Record",I8," (",A,") has no good data
       ELSE			
        WRITE(MSG,22) NREC,ACARD(1:10),N,T/N	
   22   FORMAT("Record",I8," (",A,")",I3," good, average",F9.3)	
        NB = NB + 24 - N	
        NV = NV + N		
        VTOT = VTOT + T		
      END IF			
      GO TO 10		

Complaints. Should really distinguish between trouble in the date part and in the data part.
  600 WRITE (MSG,*) '"END" declared - insufficient data?'	
      GO TO 602				
  601 WRITE (MSG,*) '"ERR" declared - improper number format?'	
  602 WRITE (MSG,603) NREC,L,ACARD(1:L)	
  603 FORMAT(" Record ",I0,", length ",I0," reads ",A)	
      HIC = HIC + 1			
      IF (HIC.LE.12) GO TO 10		
      STOP "Enough distaste."		
  666 WRITE (MSG,101) NREC,"format error
      GO TO 900				

Closedown.
  100 WRITE (MSG,101) NREC,"then end-of-file"	
  101 FORMAT (" Record ",I0,": ",A)		
      WRITE (MSG,102) NV,NB,VTOT/NV		
  102 FORMAT (I8," values, ",I0," bad. Average",F9.4)	
      IF (BBN.LE.0) THEN		
        WRITE (MSG,*) "No bad value presented, so no longest run."	
       ELSE				
        WRITE (MSG,103) BBN,BBI,BBDATE	
  103   FORMAT ("Longest bad run: ",I0,", starting hour ",I0," on ",A)	
      END IF			
  900 CLOSE(IN)		
      END