darkrabb1t
darkrabb1t

Reputation: 27

Program reads a *.csv file into an array and print contents. Require index number to be printed

I'm trying to include the index number into my println string. I've tried creating an iteration loop, but it doesn't print the index number correctly.

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 print output currently looks like:

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

I want the index number shown instead of the address location such as:

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

Upvotes: 0

Views: 56

Answers (2)

Ajay Kumar
Ajay Kumar

Reputation: 3250

IF you wish to stick to your own for loop -

 // 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++;

            }

Upvotes: 0

Not a JD
Not a JD

Reputation: 1902

The For-Each Loop you're using will iterate over the collection without exposing the index of the element you've got in your hand.

Try this instead:

//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());

}

Upvotes: 1

Related Questions