简体   繁体   English

从CSV文件读取数据时如何获取对特定数组字段的访问权限

[英]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. 我有一个带有Twitter数据的.csv文件。 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: 现在,当我使用System.out.println(twitter [2])时,他将打印第三列:

  • lat 拉特
  • 41.0338573 41.0338573
  • 40.73792159 40.73792159
  • 40.89123297 40.89123297
  • 40.79269762 40.79269762
  • 40.75165756 40.75165756
  • 40.76348875 40.76348875
  • 40.89118717 40.89118717
  • 40.73816787 40.73816787
  • 40.72321205 40.72321205
  • 40.6832726 40.6832726
  • 40.76314446 40.76314446
  • 40.7263692 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: 对于您的while循环,将其更改为:

    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 || row_counter == 1 clause to the if expression evaluation to show the header || row_counter == 1子句,用于if表达式求值以显示标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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