繁体   English   中英

如何根据用户输入从java中的链表中删除特定元素?

[英]How can I remove specific elements from a linkedlist in java based on user input?

我很新(Java 6 周)试图从一个csv 文件中删除元素,该文件在一个新行中列出了一组学生(id, name, grades)

每个学生id都按升序编号。 我想尝试通过输入 ID 号来移除学生,但我不知道该怎么做。

到目前为止,我只是尝试减少用户输入的值以匹配索引,因为学生是按数字列出的,我在一个 while 循环中做到了这一点。 但是,每次迭代都无法识别前一个用户输入的减少,我想我需要一种方法来搜索 id 的值,并从 csv 文件中删除整行。

只尝试包含相关代码。 阅读以前的堆栈问题向我展示了一堆与节点相关的答案,这对我来说毫无意义,因为我没有理解它所需的任何先决知识,而且我不确定我的其余代码是否适用于那些方法。

有什么比较简单的想法吗?

Student.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
etc....

代码:

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);

你有没有得到这个的答案,我也在为此苦苦挣扎?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM