How do i take user input and store it successfully in an ArrayList? Then how do i get my program to show me all the elements in the ArrayList?

user1999 :

I want a program that stores details about staff in an arraylist. I'd like to prompt user for input and store each result in the arraylist. How do I do this? And how do i view everything stored in the arraylist after? It doesn't need to reflect the code I have, just can't seem to figure out how i have a class with setters and getters and then create a new main class promting user for input and store that input in the arraylist.

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

public class salesPersonMain {


    public static void main(String[] args) throws InputValidationException {

        Scanner input = new Scanner(System.in);
        //ask user for input and get input
        System.out.println("Enter id: ");
        int id = Integer.parseInt(input.nextLine());

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();
        //save in array list
        List<salesPerson> sPerson = new ArrayList<salesPerson>();

        sPerson.add(new salesPerson(id, firstName, lastName));

    }
}

I have another class for the salesperson:

import java.util.ArrayList;

public class salesPerson<sPerson> {
    //create variables for sales person
    private int id;
    private String firstName;
    private String lastName;
 public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) throws InputValidationException {
        if (firstName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.firstName = firstName;
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName)throws InputValidationException {
        if (lastName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.lastName = lastName;
        }
    }
 //creates array of salespeople
        private ArrayList<sPerson> salesPerson;

        public salesPerson() {
            salesPerson = new ArrayList<>();
        }
        //adds new salesperson to the array
        public void add(salesPerson sPerson) {
            salesPerson.add((sPerson) sPerson);
        }
Johnny Mopp :

You'll need a loop to repeatedly get the input:

public static void main(String[] args) throws InputValidationException {

    Scanner input = new Scanner(System.in);
    List<salesPerson> sPerson = new ArrayList<salesPerson>();

    // Loop forever
    // Need a way to break the loop. One option: have the user
    // input "q" for quit
    while (true) {

        //ask user for input and get input
        System.out.println("Enter id ('q' to quit): ");
        String temp = input.nextLine();
        if (temp.equals("q")) break;

        int id = Integer.parseInt(temp);
           // This should be in try/catch in case parseInt fails

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();

        //save in array list
        sPerson.add(new salesPerson(id, firstName, lastName));
    }

    // Print the list
    sPerson.forEach(System.out::println);
}

So it prints out properly, you need to override the toString function in the salesPerson class:

public class salesPerson {
    // Other code here.....

    @Override
    public String toString() {
        return id + "," + firstName + " " + lastName;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157573&siteId=1