Jes
Jes

Reputation: 19

How do I get my java ArrayList to display properly?

I have a class where a user inputs information about an employee (first and last name, address, hire date) for a user determined number of employees.

import java.util.ArrayList;
import java.util.Scanner;

public class Employee {
    public static void main(String [] args){
        String employeeName = null;
        String employeeAddress = null;
        String hireDate = null;

        Scanner userInput = new Scanner(System.in);
        System.out.println("How many employees would you like to enter information for?");
        int numEmployees = userInput.nextInt();

        for( int i = 0; i < numEmployees; i++) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter employees first name: ");
            String firstName = input.nextLine();
            System.out.println("Enter employees last name: ");
            String lastName = input.nextLine();
            System.out.println("Enter street employee lives on:");
            String street = input.nextLine();
            System.out.println("Enter city employee lives in:");
            String city = input.nextLine();
            System.out.println("Enter state employee lives in:");
            String state = input.nextLine();
            System.out.println("Enter employee's zip code:");
            String zip = input.nextLine();
            System.out.println("Enter month employee was hired:");
            String month = input.nextLine();
            System.out.println("Enter day employee was hired:");
            String day = input.nextLine();
            System.out.println("Enter year employee was hired:");
            String year = input.nextLine();

            Name name = new Name(firstName, lastName);
            Address address = new Address(street, city, state, zip);
            Date date = new Date(month, day, year);

            employeeName = name.getName();
            employeeAddress = address.getAddress();
            hireDate = date.getDate();

            ArrayList<String> obj = new ArrayList<String>();
            obj.add(employeeName);
            obj.add(employeeAddress);
            obj.add(hireDate);
            System.out.println(obj);
        }
    }
}

I'm trying to get the program to go through the user prompts, take that info and put it in a position in an ArrayList, then repeat for however many times the user determined earlier and display all at once at the end. Something like this:

FirstName LastName, Address, Hire Date
FirstName LastName, Address, Hire Date

And so on. Right now, my code will run through the user prompts, display that, then run through the next round of prompts and display that:

Enter Name:
 Name
Enter Address:
 Address
Enter Hire Date:
 Hire Date

[Name, Address, Hire Date]

Enter Name:
 Name
Enter Address:
 Address
Enter Hire Date:
 Hire Date

[Name, Address, Hire Date]

I realize this is because my array and array display are within the for loop, but when I move the array outside the loop I get no display. When I move just the array display outside the loop, my code doesn't compile.

Upvotes: 0

Views: 181

Answers (2)

Ian
Ian

Reputation: 30813

Move this line:

System.out.println(obj);

out of the for loop. And use List to contain the ArrayList. Then you display it in a separate for loop:

List<ArrayList<String>> objs = new ArrayList<ArrayList<String>>(); //use list of objs
for( int i = 0; i < numEmployees; i++ )
{
    //all the inputs

    ArrayList<String> obj = new ArrayList<String>();
    obj.add(employeeName);
    obj.add(employeeAddress);
    obj.add(hireDate);
    objs.add(obj);

}

for( int i = 0; i < numEmployees; i++ ) //for loop just to display
    System.out.println(objs.get(i)); //display each element here

Upvotes: 2

Jose Martinez
Jose Martinez

Reputation: 11992

What you are printing out is a tuple of type [string, string, string]. This can be represented as a List. Then for each employee you want to store this tuple in another List of type List<List<String>>.

public static void main( String [] args )
{

    String employeeName = null;
    String employeeAddress = null;
    String hireDate = null;

    Scanner userInput = new Scanner(System.in);
    System.out.println("How many employees would you like to enter information for?");
    int numEmployees = userInput.nextInt();
    List<List<String>> employesInfo = new ArrayList<List<String>>();

    for( int i = 0; i < numEmployees; i++ )
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter employees first name: ");
        String firstName = input.nextLine();
        System.out.println("Enter employees last name: ");
        String lastName = input.nextLine();
        System.out.println("Enter street employee lives on:");
        String street = input.nextLine();
        System.out.println("Enter city employee lives in:");
        String city = input.nextLine();
        System.out.println("Enter state employee lives in:");
        String state = input.nextLine();
        System.out.println("Enter employee's zip code:");
        String zip = input.nextLine();
        System.out.println("Enter month employee was hired:");
        String month = input.nextLine();
        System.out.println("Enter day employee was hired:");
        String day = input.nextLine();
        System.out.println("Enter year employee was hired:");
        String year = input.nextLine();

        Name name = new Name(firstName, lastName);
        Address address = new Address(street, city, state, zip);
        Date date = new Date(month, day, year);

        employeeName = name.getName();
        employeeAddress = address.getAddress();
        hireDate = date.getDate();

        ArrayList<String> obj = new ArrayList<String>();
        obj.add(employeeName);
        obj.add(employeeAddress);
        obj.add(hireDate);

        employeesInfo.add(obj);
    }

    for (int i = 0; i < numEmployees; i++ ) {
        System.out.println(emploeesInfo.get(i));
    }
}

Upvotes: 0

Related Questions