简体   繁体   中英

Get a word from a specific location in a text file in java

I am currently doing an algorithms project where we have to use two text files to fill in the blanks with the missing words. One of the txt files contains a whole script of a movie called "The Truman Show" while the other txt file contains sentences (statements) with missing words which are indicated by ' '.I have found the missing word from the Script.txt and I know how to that I am supposed to use replaceAll to replace ' ' with the specific word.

I am using BufferedReader to scan both txt files and both one buffered reader has a pointer i and the other buffered reader has a pointed j. i stops at the beginning of the missing word, how am I supposed to use the pointer j to obtain the missing word.

while(i < Movie_Script_Length)
        {
            if(Current_Statement.charAt(j) == Movie_Script.charAt(i))
            {
                i++; 
                j++; 
            }
            else if(Current_Statement.charAt(j) != Movie_Script.charAt(i) && Current_Statement.charAt(j) == Underscore)
            {
                //Replace the underscores with the word
                Current_Statement.replaceAll(regex, replacement); 
                //System.out.println("Missing word found for Statement " + Statement_Number); 
            }

I would use a Scanner to go trough the files and check per scanned line what the contents are with 'contains' via String. This way you can check for words you need to find and insert them into a new textfile without storing the entire thing in memory.

I'd use the method described here: https://www.java-tips.org/scanning-text-with-java-util-scanner.html

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