【问题标题】:Program reads a *.csv file into an array and print contents. Require index number to be printed程序将 *.csv 文件读入数组并打印内容。要求打印索引号
【发布时间】:2019-04-02 14:38:16
【问题描述】:

我正在尝试将索引号包含在我的 println 字符串中。我试过创建一个迭代循环,但它没有正确打印索引号。

package main;
import test.address;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Lab9_main {

    // Delimiters used in the CSV file
    private static final String COMMA_DELIMITER = ",";

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            // Reading the csv file
            br = new BufferedReader(new FileReader("addresses.csv"));

            // Create List for holding address objects
            ArrayList<address> addressList = new ArrayList<>();

            String line;

            // Read to skip the header
            br.readLine();

            // Reading from the second line
            while ((line = br.readLine()) != null) {
                String[] addressDetails = line.split(COMMA_DELIMITER);

                //Save the address details in address object
                if(addressDetails.length > 0 ) {
                    address addy = new address(addressDetails[0], addressDetails[1], addressDetails[2],
                            addressDetails[3], addressDetails[4], Integer.parseInt(addressDetails[5]));
                    addressList.add(addy);
                }
            }

            // Lets print the address List
            for(address e : addressList) {
                    System.out.println("The address details in the index....." + e + "....:" + e.getFirstName()
                            + "..." + e.getLastName() + "..." + e.getAdd() + "...." + e.getCit() + ".. " + e.getSt()
                            + "..." + e.getZip());

            }

        } catch(Exception ee) {
            ee.printStackTrace();
        }

        finally {
            try {
                br.close();
            } catch(IOException ie) {
                System.out.println("Error occurred while closing the BufferedReader");
                ie.printStackTrace();
            }
        }
    }
}

当前的打印输出如下:

The address details in the index.....test.address@61bbe9ba....:John...Doe...120 jefferson st.....Riverside..  NJ...80751
The address details in the index.....test.address@610455d6....:Jack...McGinnis...220 hobo Av.....Phila..  PA...9119

我想要显示的索引号而不是地址位置,例如:

The address details in the index.....0....:John...Doe...120 jefferson st.....Riverside..  NJ...80751
The address details in the index.....1....:Jack...McGinnis...220 hobo Av.....Phila..  PA...9119

【问题讨论】:

    标签: java csv arraylist indexing printing


    【解决方案1】:

    您使用的For-Each Loop 将遍历集合,而不会暴露您手中的元素的索引。

    试试这个:

    //Lets print the Employee List
    for(int i = 0; i < addressList.size(); i++) {
            address e = addressList.get(i);
            System.out.println("The address details in the index....." + i + "....:" + e.getFirstName()
                + "..." + e.getLastName() + "..." + e.getAdd() + "...." + e.getCit() + ".. " + e.getSt()
                + "..." + e.getZip());
    
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望坚持自己的 for 循环 -

       // Lets print the address List
      
           // initialize your index number 
      
               int i=0;
      
                  for(address e : addressList) {
                          System.out.println("The address details in the index....." + i + " "  + e + "....:" + e.getFirstName()
                                  + "..." + e.getLastName() + "..." + e.getAdd() + "...." + e.getCit() + ".. " + e.getSt()
                                  + "..." + e.getZip());
      
           // increment your index and let it roll
      
                    i++;
      
                  }
      

      【讨论】:

      • 此代码不起作用。它打印“0”作为列表中所有地址的索引号。
      • 您可以在 for 循环之外增加“i”。应该工作。
      猜你喜欢
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多