简体   繁体   English

在文本文件中搜索字符串并了解它是哪一行

[英]Searching a string in a text file and learning which line is it

I have a text file like down below:我有一个如下的文本文件:

jack;杰克; 488;22;98 488;22;98

kylie;凯莉; 541;72;81 541;72;81

jenna;珍娜; 995;66;17. 995;66;17。

. .

The list is formatted as follows:该列表的格式如下:
On every line, the first number after the name is the student's code and the numbers following it are scores.在每一行,名字后面的第一个数字是学生的代码,后面的数字是分数。

I want to pass the student's code (as a String ) as the input to the program and it should return the student's second score to me.我想将学生的代码(作为String )作为输入传递给程序,它应该将学生的第二个分数返回给我。

I have tried bufferedreader ,but I can just write all text files as output, but I can't search for the code and the other things.我试过bufferedreader阅读器,但我可以将所有文本文件写为 output,但我无法搜索代码和其他内容。

Thanks谢谢

BufferedReader br = new BufferedReader(new FileReader("filePath"));

String contentLine = br.readLine();

while (contentLine != null) {
    String[] result=contentLine.split(";");
    String studentCode =result[1].trim();
    // apply your logic for studentCode here
    contentLine = br.readLine();
}
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FilterCsv {

    private class Student {
        private String name;
        private String code;
        private String score;
        private String score2;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getScore() {
            return score;
        }

        public void setScore(String score) {
            this.score = score;
        }

        public String getScore2() {
            return score2;
        }

        public void setScore2(String score2) {
            this.score2 = score2;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", code='" + code + '\'' +
                    ", score='" + score + '\'' +
                    ", score2='" + score2 + '\'' +
                    '}';
        }
    }

    private Function<String, Student> mapToItem = (line) -> {
        System.out.println(line);
        String[] p = line.split(";");
        Student student = new Student();
        student.setName(p[0]);
        if (p[1] != null && p[1].trim().length() > 0) {
            student.setCode(p[1]);
        }
        if (p[2] != null && p[2].trim().length() > 0) {
            student.setScore(p[2]);
        }
        if (p[3] != null && p[3].trim().length() > 0) {
            student.setScore2(p[3]);
        }
        return student;
    };

    private List<Student> processInputFile(String inputFilePath, String name) {
        List<Student> inputList = new ArrayList<>();
        try {
            File inputF = new File(inputFilePath);
            InputStream inputFS = new FileInputStream(inputF);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));
            // skip the header of the csv
            inputList = br.lines().map(mapToItem).collect(Collectors.toList());
            br.close();
            String secondScore = inputList
                    .stream()
                    .peek(System.out::println)
                    .filter((s -> s.getName().equals(name)))
                    .findFirst()
                    .get().getScore2();
            System.out.println("Score 2 for " + name + " is: " + secondScore);
        } catch (IOException e) {
            System.out.println(e);
        }
        return inputList;
    }

    public static void main(String[] args) {
        new FilterCsv().processInputFile("your filepath, "studentsName");
    }
}

add some error checking and stuff... Cheers添加一些错误检查和东西......干杯

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

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