如何根据用户输入从java中的linkedlist中删除特定元素?
我是一个新手(进入java 6周),我尝试从csv文件中删除元素,该文件列出了一组学生,例如如何根据用户输入从java中的linkedlist中删除特定元素?,java,linked-list,Java,Linked List,我是一个新手(进入java 6周),我尝试从csv文件中删除元素,该文件列出了一组学生,例如(id、姓名、成绩),每个学生都在一个新行上 每个学生id都按升序编号。我想通过输入id号来删除一名学生,但我不确定如何才能做到这一点 到目前为止,我只是尝试减少用户输入的值,以匹配学生按数字列出的索引,我是在while循环中这样做的。但是,每次迭代都无法识别以前用户输入的减少,我想我需要一种方法,可以只搜索id的值,并从csv文件中删除整行内容 仅尝试包含相关代码。阅读前面的堆栈问题向我展示了一系列与节
(id、姓名、成绩)
,每个学生都在一个新行上
每个学生id
都按升序编号。我想通过输入id号来删除一名学生,但我不确定如何才能做到这一点
到目前为止,我只是尝试减少用户输入的值,以匹配学生按数字列出的索引,我是在while循环中这样做的。但是,每次迭代都无法识别以前用户输入的减少,我想我需要一种方法,可以只搜索id的值,并从csv文件中删除整行内容
仅尝试包含相关代码。阅读前面的堆栈问题向我展示了一系列与节点相关的答案,这些答案对我来说毫无意义,因为我不具备理解它所需的任何先决知识,而且我不确定我的代码的其余部分是否适用于这些方法
有没有相对简单的想法
Student.txt(每一个都在新行上)
代码:
public static boolean readFile(String filename) {
File file = new File("C:\\Users\\me\\eclipse-workspace\\studentdata.txt");
try {
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()) {
String[] words=scanner.nextLine().split(",");
int id = Integer.parseInt(words[0]);
String firstName = words[1];
String lastName = words[2];
int mathMark1 = Integer.parseInt(words[3]);
int mathMark2 = Integer.parseInt(words[4]);
int mathMark3 = Integer.parseInt(words[5]);
int englishMark1 = Integer.parseInt(words[6]);
int englishMark2 = Integer.parseInt(words[7]);
int englishMark3 = Integer.parseInt(words[8]);
addStudent(id,firstName,lastName,mathMark1,mathMark2,mathMark3,englishMark1,englishMark2,englishMark3);
}scanner.close();
}catch (FileNotFoundException e) {
System.out.println("Failed to readfile.");
private static void removeStudent() {
String answer = "Yes";
while(answer.equals("Yes") || answer.equals("yes")) {
System.out.println("Do you wish to delete a student?");
answer = scanner.next();
if (answer.equals("Yes") || answer.equals("yes")) {
System.out.println("Please enter the ID of the student to be removed.");
//tried various things here: taking userInput and passing through linkedlist.remove() but has never worked.
这个解决方案可能不是最优的或漂亮的,但它是有效的。它逐行读取输入文件,将每一行写入一个临时输出文件。每当它遇到一行与您要查找的内容相匹配的内容时,它都会跳过该行。然后重命名输出文件。我在示例中省略了错误处理、关闭读写器等。我还假设您要查找的行中没有前导或尾随空格。根据需要更改trim()的代码,以便找到匹配项
File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "bbb";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
提供studentdata.txt示例文件,包含少量记录。1,Frank,West,98,95,87,78,77,80 2,Dianne,Greene,78,94,88,87,95,92 3,Doug,Lei,78,94,88,87,95,92您是否尝试过使用任何java库(如CSVReader或Apache Commons CSVThanks)来实现它,但是,我只想编辑linkedlist,并且不一定影响studentdata.txt文件,直到很久以后。因此,数据最初被提取并保存到一个临时链接列表中,该列表被读取、编辑(并从另一个方法写入文件)。虽然从内存的角度来看,它显然不是最优的,但列表非常小。您正在谈论的链接列表是什么,您的代码没有显示任何列表,您能否提供包含您正在谈论的链接列表的代码。读取文件,提取每个名称等,并保存为名为students的临时链接列表。LinkedList本身仅在其上执行转换为等级等功能时才会打印出来,但LinkedList本身是从csv文件派生的。LinkedList称为LinkedList学生;从另一个类派生,该类为每个学生从文件中读取时要执行的每个方法设置参数。因此,仅在读取和输出csv文件时才提供该列表。我不确定我是否解释得很好。从链表中删除元素非常容易。像这样使用Java流:list.stream.filter(e->e.getId()!=unwantedValue.collector(Collectors.toList());
File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "bbb";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);