read data from a file in java

后端 未结 5 1147
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 04:32

I have a text file in the following format:

Details.txt

The file is a .txt file. I want to read course title from this file and print corresponding textbook

5条回答
  •  天涯浪人
    2021-01-17 05:30

    1. First read the file correctly line by line, and search for your entered course title, lets consider "Java"

    2. Now you hit your title and you know you need 3 consecutive lines from your file as all information related to that title are there.

      if(str.startsWith(title)); {  // for title = "Java"
        line1 = 1st line  // contains ISBN and First Name
        line2 = 2nd line  // Title and Last Name
        line3 = 3rd line  // Author and Department
        line4 = 4th line  // Email
        break;  // this will take you out of while loop
      }
      
    3. Now on those four lines do string operations and extract your data as you need and use it.

    I am home so I can't give you exact code. But if you follow this it will solve your issue. Let me know if any problem you got while doing this.

    Follow this to get some info on String operations

提交回复
热议问题