【发布时间】:2021-06-13 05:17:50
【问题描述】:
我正在学习 JAVA 并编写一个基本程序,并试图找出一种方法来过滤我的 CSV 中的现有记录,并根据用户输入将新记录添加到我的 CSV 文件中。当用户输入所需的输入时,我正在检查用户输入是否与 CSV 文件中的记录匹配。我想显示相关输入的所有匹配记录。我有不同的方法来完成这项工作。我还创建了一个单独的方法,该方法应将输入的任何新记录添加到 CSV 文件中。为此,我在下面做-
import java.util.Scanner;
public class FilterAndAddEmployeeData {
ArrayList<Employee>employeeList; // Employeee class is a POJO here
Scanner userInput = new Scanner(System.in);
File file = new File("data.csv");
Employee emp = new Employee(); // This will expect parameters similar to POJO I believe
public void findByName(String fName, String lName) {
File file = new File(data.csv);
Scanner x = null;
System.out.println("Enter first name:")
String fName = x.next();
System.out.println("Enter last name:")
String lName = x.next();
while(x.hasNextLine()) {
String fileData = x.next();
String inputStream = fileData.split(',');
for(String i: inputStream){
System.out.println(i); // This will print all 5 rows of my CSV
}
}
}
public void findById(String id) {
// TO - DO
}
public void addEmployee(Employee emp){
employeeList.add(emp); // To add employee details inputted by user in the employee object
}
}
这里,我的 Employee 类是一个单独的类,它只有 getter 和 setter 以及成员数据。我在 main() 中调用了这三个单独的方法。 我可以在没有逗号的情况下打印我的 CSV 文件。问题是我无法根据用户输入从 CSV 过滤记录。例如,如果我输入 first name 和 last name ,控制台应该从我的 CSV 打印相应的记录,否则返回 null。有人可以帮我理解这一点吗?我的 CSV 文件有 5 行,其中包含名字、姓氏、年龄、员工 ID 字段。
编辑(根据以下建议):
public void findByName(String fName, String lName) {
Scanner userInput = new Scanner(System.in);
File file = new File("data.csv");
try {
fileScanner = new Scanner(file);
fileData = userInput.nextLine();
System.out.print("Enter first name: --> ");
String fName = userInput.nextLine().trim();
System.out.print("Enter last name: --> ");
String lName = userInput.nextLine().trim();
// List<String> foundRecords = new ArrayList<>();
boolean found = false;
while (fileScanner.hasNextLine()) {
fileData = userInput.nextLine().trim();
// Skip blank lines (if any).
if (fileData.isEmpty()) {
continue;
}
/* The 'Regular Expression' (regex) to use in the String#split() method.
This will handle any spacing around the comma delimiter when splitting.
This eliminate the need to carry out array element trimming of leading
or trailing whitespaces. */
String regex = "\\s*,\\s*";
String[] lineParts = fileData.split(regex);
/* Based on the header line information we know that First Name
data is going to be in column index 0 and the Last Name data
is going to be in column index 1. */
found = (fName.isEmpty() && lName.isEmpty()) ||
(lineParts[0].equalsIgnoreCase(fName) && lName.isEmpty()) ||
(fName.isEmpty() && lineParts[1].equalsIgnoreCase(lName)) ||
(lineParts[0].equalsIgnoreCase(fName) && lineParts[1].equalsIgnoreCase(lName));
Employee emp = new Employee(lineParts[0],lineParts[1], lineParts[2])
if (found) {
employeeList.add(emp);
found = false;
}
}
// Display found records (if any)
System.out.println();
System.out.println("Found Records:");
System.out.println("====================================");
if (employeeList.isEmpty()) {
System.out.println(" No Records Found!");
}
else {
for (Employee str : employeeList) {
System.out.println(str);
}
}
System.out.println("====================================");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
这是真正的代码sn-p吗?您的
file似乎没有使用它。