Method to find string inside of the text file. Then getting the following lines up to a certain limit

前端 未结 7 590
孤街浪徒
孤街浪徒 2020-12-08 15:35

So this is what I have so far :

public String[] findStudentInfo(String studentNumber) {
                Student student = new Student();
                Scan         


        
7条回答
  •  难免孤独
    2020-12-08 16:10

    When you are reading the file, have you considered reading it line by line? This would allow you to check if your line contains the file as your are reading, and you could then perform whatever logic you needed based on that?

    Scanner scanner = new Scanner("Student.txt");
    String currentLine;
    
    while((currentLine = scanner.readLine()) != null)
    {
        if(currentLine.indexOf("Your String"))
        {
             //Perform logic
        }
    }
    

    You could use a variable to hold the line number, or you could also have a boolean indicating if you have passed the line that contains your string:

    Scanner scanner = new Scanner("Student.txt");
    String currentLine;
    int lineNumber = 0;
    Boolean passedLine = false;
    while((currentLine = scanner.readLine()) != null)
    {
        if(currentLine.indexOf("Your String"))
        {
             //Do task
             passedLine = true;
        }
        if(passedLine)
        {
           //Do other task after passing the line.
        }
        lineNumber++;
    }
    

提交回复
热议问题