java - 如何获取用户输入并将其成功存储在 ArrayList 中?那么如何让我的程序向我展示 ArrayList 中的所有元素?
问题描述
我想要一个将员工详细信息存储在数组列表中的程序。我想提示用户输入并将每个结果存储在数组列表中。我该怎么做呢?之后如何查看存储在数组列表中的所有内容?它不需要反映我拥有的代码,只是似乎无法弄清楚我如何拥有一个带有 setter 和 getter 的类,然后创建一个新的主类提示用户输入并将该输入存储在 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);
}
解决方案
您需要一个循环来重复获取输入:
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);
}
因此它可以正确打印出来,您需要覆盖类toString
中的函数salesPerson
:
public class salesPerson {
// Other code here.....
@Override
public String toString() {
return id + "," + firstName + " " + lastName;
}
}
推荐阅读
- r - `rhandsontable` 包中的 `hot_to_r()` 函数在“正常”.R 脚本中不起作用 - 如何检查闪亮数据的方面?
- amazon-web-services - AWS CodeBuild 事件不会触发 lambda
- java - 即使使用 sharedPreferences,AlertDialog 也永远不会显示
- node.js - ESLint 同时输出到文件和控制台
- snowflake-cloud-data-platform - 从雪花外部阶段识别已处理的文件
- c++ - 有没有办法有效地更新 QML 中的 CAN 数据?
- c++ - 在 CGAL 中使用 Efficient_Ransac 时出现问题
- git - 何时避免执行 Git rebase
- apache-kafka - 在 Apache Storm 中处理 kafka 消息时如何确保只使用一次语义
- spring-statemachine - spring-statemachine 2.3.1 - 如何从 StateMachineModelFactory 获取 StateMachine?