简体   繁体   中英

Java | Finding specific lines from file

So I'm currently working on a project from school. I've saved data from customers in a .txt file in this format

-----
17-03-2020 15:49
WashType: De Luxe
ID: 1, Name: Janus Pedersen
-----
-----
20-03-2020 13:07
WashType: Standard
ID: 2, Name: Hardy Akira
-----

In order to thank customers for using this service, I'd like to give a customer some cinema tickets after each 10th purchase from us. To do that, I thought of reading this file again and look for their ID and count that but I simply can't make that work. My initial idea was something like the following, but it keeps giving me a null pointer

    String[] words;  
    FileReader fr = new FileReader("stats.txt");  
    BufferedReader br = new BufferedReader(fr); 
    String s;
    String input = String.valueOf(washCard.getCardID());   
    int count=0;   
    while((s=br.readLine())!=null)   
    {
        words=s.split(" ");  
        for (String word : words)
        {
            if (word.equals(input))   
            {
                count++;    
                System.out.println(word);
            }
        }
    }

Anyone who has any great ideas for this? For things to be easier I've added it all to a github repo: https://github.com/rasm937k/curly-broccoli

You can use Java 8 streams for that:

Files.lines(Paths.get("stats.txt"))
        .map(line -> line.split(" "))
        .filter(words -> words[5].equals(washCardId))
        .count();

Also here's a nice tutorial on Java 8 Streams: https://www.baeldung.com/java-8-streams

The below code is based on Michał Kaciuba's answer but adapted to suit the actual format of your stats.txt file. I didn't know how to post this as a comment, hence I am posting it as an answer, but as I said, Michał Kaciuba should get the credit and I think you should accept his answer. Note that explanation of the code follows the actual code.

String input = String.valueOf(washCard.getCardID());
Pattern pttrn = Pattern.compile("^ID: (\\d+)");
Path p = Paths.get("stats.txt");
try {
    long count = Files.lines(p)  //throws java.io.IOException
                      .filter(l -> {Matcher mtchr = pttrn.matcher(l); return mtchr.find() && input.equals(mtchr.group(1));})
                      .count();
    System.out.println(count);
}
catch (IOException x) {
    x.printStackTrace();
}

Files.lines(p) creates a Stream where every element in the stream is a line from file stats.txt , ie a String .

The regular expression matches lines that start with ID: followed by a single space, followed by a series of one or more digits. The digits part is known as a capturing group because it is enclosed in parentheses.

The filter() checks to see if the line from the file matches the regular expression and if it does, the filter() then checks whether the "digits" in that line match your input , ie String.valueOf(washCard.getCardID()) .

The count() counts all the elements in the stream returned by filter() and count() returns a long .

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