0

My text file looks like this

 STID  STNM  TIME     TMAX TMAXO     TMIN TMINO    TAVG     TBAD     DMAX DMAXO     DMIN DMINO    DAVG    VDEF     DBAD     SMAX SMAXO     SMIN SMINO    SAVG     SBAD     BMAX BMAXO     BMIN BMINO    BAVG     BBAD     S5MX S5MXO     S5MN S5MNO    S5AV     S5BD     S25X S25XO     S25N S25NO   S25AV    S25BD     S60X S60XO     S60N S60NO   S60AV    S60BD     HMAX HMAXO     HMIN HMINO    HAVG     HBAD     PMAX PMAXO     PMIN PMINO    PAVG    MSLP     PBAD     AMAX AMAXO     ATOT     ABAD     PDIR     PDFQ     SDIR     SDFQ     IBAD     WSMX WSMXO     WSMN WSMNO    WSPD    WDEV    WMAX WMAXO     WBAD     RAIN     RNUM    RMAX     RBAD    9AVG     9BAD     2MAX     2MIN     2AVG     2DEV     2BAD     HDEG     CDEG     HTMX HTMXO    HTBAD     WCMN WCMNO    WCBAD
 ACME   110     0    76.32   131    69.22   184   71.57        0    69.10   286    61.55     3   66.48    4.22        0    83.16     3    78.24   288   80.85        0    85.37     3    77.74   288   81.77        0    83.12   150    77.86   288   80.58        0    83.84     3    81.23   288   82.34        0    81.54     3    80.94   285   81.29        0    96.82   278    66.82     1   84.59        0    28.74   284    28.67    23   28.71   30.10        0   412.73   130     5.46        0     -996     -999     -996     -999       59    10.92   132     0.00    37    4.34    2.41   14.61   146        0     0.22       19    0.24        0   71.67        0     8.44     0.00     2.49     2.30        0     0.00     7.77     -996   999      288     -996   999      288
 ADAX     1     0    73.99    96    68.61    21   71.32        0    70.91   169    62.77     1   68.22    2.58        0    87.15     3    82.99   288   84.83        0    88.32     3    79.54   288   83.59        0    85.06     3    81.84   288   83.31        0    88.48     3    85.21   288   86.61        0     -996   999     -996   999    -996       96    98.40   274    73.27     1   90.20        0    29.08   137    29.01    17   29.04   30.08        0   210.42   151     5.23        0     -996     -999     -996     -999      139    12.83   106     0.00    33    3.65    3.03   19.28   121        0     0.24       23    0.24        0   71.57        0     8.84     0.00     2.07     2.48        0     0.00     6.30     -996   999      288     -996   999      288
 ALTU     2     0    75.51   107    68.74   168   71.63        0    70.43   279    64.56   125   67.48    3.50        0    80.60     3    77.88   288   78.91        0    79.11     3    75.96   288   77.08        0    79.97     3    77.23   288   78.41        0    81.95     3    79.57   288   80.55        0     -996   999     -996   999    -996       96    98.36   286    70.28   106   87.18        0    28.68   276    28.60    51   28.64   30.09        0   202.20   123     5.03        0        2    30.80        4    18.63       25    13.72   128     0.00    70    5.79    2.71   18.19   128        0     0.19       19    0.12        0   71.53        0     9.55     0.00     3.71     2.22        0     0.00     7.12     -996   999      288     -996   999      288

I have used the following code snippet to store the text line by line in an array list and displays the entire file as output.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Stack {

    public static void main(String[] args) throws IOException {


    // create token1
    String token1 = "";

    /

    // create Scanner inFile1
    Scanner inFile1 = new Scanner(new File("daily.txt")).useDelimiter(",\\s*");



    List<String> temps = new ArrayList<String>();

    // while loop
    while (inFile1.hasNext()) {
      // find next line
      token1 = inFile1.next();
      temps.add(token1);
    }
    inFile1.close();

    String[] tempsArray = temps.toArray(new String[0]);

    for (String s : tempsArray) {
      System.out.println(s);
    }
  }
}

But I want to display only three columns TMAX, TMIN,DMAX.

3
  • What is the question? Commented Aug 11, 2017 at 0:19
  • suppose you need to find a work around with temps.get(INDEX) with INDEX is the index of element Commented Aug 11, 2017 at 0:20
  • What's your problem here? You don't know how to break up a line? You want to know how to break up a string by delimiter? You want to break up a line by width? You want to know how to store each broken-up line? You want to know how to access a field from the "broken-up" line? Commented Aug 11, 2017 at 1:50

2 Answers 2

1

That looks like a tab separated value file - how about splitting the line on tabs?

// Make this a list of arrays of Strings
List<String[]> temps = new ArrayList<String[]>(); 

// while loop
while (inFile1.hasNext()) {
  // find next line
  token1 = inFile1.next();
  temps.add(token1.split('\t')); // split the line on tab characters
}
inFile1.close();
// this is not necessary for your implementation or mine
// String[] tempsArray = temps.toArray(new String[0]);

for (String[] s : temps) {
  // TMAX, TMIN, and DMAX are the 4th, 6th, and 10th items in each row respectively
  System.out.println(s[3] + " " + s[5] + " " + s[9]);
}

If you want something more robust, you can use a CSV / TSV reader with a dictionary interface - this lets you get each row as a map from [column name] to [column value in that row]

Sign up to request clarification or add additional context in comments.

Comments

0

If these columns are bound to each other with some (probably nested) business rule (object graph) and will be useful within application, then you can go with Object Oriented approach by mapping Every Row as an Object and columns as different attributes (with containment of child objects). e.g.

public class Row {
    private String Stid;    // choose appropriate name and type for each
    private int STNM;
    ...
    //probably (TMAX, TMAXO, TMIN, TMINO) being closely related may go as 
    // contained object
    //  ...
    //appropriate object graph
}

This way complete data is mapped to Objects in collection (may be List), then we can use it way we want by iteration, comparison, sorting etc.

If these fields are are not special to application but just as String tokens then as already suggested, two dimentional List is better option.

If its only purpose is to filter the data then while reading itself we can ignore un-required columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.