简体   繁体   中英

How to get acces to a specific array field when data read in from a csv-file

I have a .csv-File with twitter data. There are rows and columns.

My code to read it in is:

  public static void main(String[] args) {

    String csvFile = "twitter.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ";";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
        String twitter[] = line.split(cvsSplitBy);

        System.out.println(twitter[2]);

        }

    }

Now when i use System.out.println(twitter[2]) he will print the third column:

  • lat
  • 41.0338573
  • 40.73792159
  • 40.89123297
  • 40.79269762
  • 40.75165756
  • 40.76348875
  • 40.89118717
  • 40.73816787
  • 40.72321205
  • 40.6832726
  • 40.76314446
  • 40.7263692

My question: How can i access one specific field in my array. I don't want the whole column printed. I just want one number of the column.

for your while loop, change it to this:

    int row_desired = 123;
    int row_counter = 0;
    while ((line = br.readLine()) != null) {
        if (row_counter++ == row_desired) {
            String twitter[] = line.split(cvsSplitBy);
            System.out.println(twitter[2]);
        }
    }

note, that will leave out your column header, add a || row_counter == 1 || row_counter == 1 clause to the if expression evaluation to show the header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM