简体   繁体   中英

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

I'm very new (6 weeks into java) trying to remove elements from a csv file that lists a set of students as such (id, name, grades) each on a new line.

Each student id is numbered in ascending value. I want to try and remove a student by entering the id number and I'm not sure how I can do this.

So far I've just tried to reduce the value that user inputs to match the index as students are listed by number and I did this in a while loop. However, each iteration doesn't recognize the reduction from the previous user Input, and I think I need a way that can just search the value of the id, and remove the entire line from the csv file.

Have only tried to include the pertinent code. Reading previous stack questions has shown me a bunch of answers related to nodes, which make no sense to me since I don't have whatever prerequisite knowledge is required to understand it, and I'm not sure the rest of my code is valid for those methods.

Any ideas that are relatively simple?

Student.txt (each on a new line)

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

Code:

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.

This solution may not be optimal or pretty, but it works. It reads in an input file line by line, writing each line out to a temporary output file. Whenever it encounters a line that matches what you are looking for, it skips writing that one out. It then renames the output file. I have omitted error handling, closing of readers/writers, etc. from the example. I also assume there is no leading or trailing whitespace in the line you are looking for. Change the code around trim() as needed so you can find a match.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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