Search in a file for Specific sentences in a txt file in java

Aby W

I have a TXT file with a multiple choice question and answer(like 150 question),this is the format:

  1. what's your name? a. danny b. pedro c. jose d. mikey

I need to seek in the file and get the questions and the answer to show them in a UI interface.

For the moment, I can read and print the file, but I don't know how to get the sentence for separate. Any suggestion?

The code:

import java.io.*;
import java.nio.file.Path;

public class fileManager {

public FileInputStream inputStream;
public InputStreamReader reader;
public File myfile;
public String question;
public String [] answer;


public fileManager(String myfile) {
    this.myfile = new File(String.valueOf(myfile));
    try {
        inputStream = new FileInputStream(this.myfile );
        try {
            reader = new InputStreamReader(inputStream , "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public void printFile(){
    int indexChar = 1;
    char concatination = '.';
    int endFile = 0;
    try {
        endFile = inputStream.available();
    } catch (IOException e) {
        e.printStackTrace();
    }
    do {
        try {
            char mychar = (char)reader.read();
            if (mychar == ((char)indexChar)){
                if(concatination == (char)reader.read()){
                    do{
                        System.out.print((char)reader.read());
                    }while ((char)reader.read() == 'א');
                }
            }
            endFile++;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }while(endFile < 1000);
}

public void closeFile(){
    try {
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
} //End function

public void getChar(){


}


public void getTheQuestion(){
    int questionNum = 0;
    int eof = 0;
    int i =0;
    String []file;
    String question;

    try {
        eof = inputStream.available();
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (;i == '1';){

        try {
            i = reader.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}//end  getTheQuestion


//Getters and Setters
public FileInputStream getInputStream() {
    return inputStream;
}

public void setInputStream(FileInputStream inputStream) {
    this.inputStream = inputStream;
}

public File getMyfile() {
    return myfile;
}

public void setMyfile(File myfile) {
    this.myfile = myfile;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public String[] getAnswer() {
    return answer;
}

public void setAnswer(String[] answer) {
    this.answer = answer;
}

}
afzalex

You can accomplish it with regular expressions. Here I have written a program to help.
I have created Pattern's for questions and all four options and then fetched them.

File file = new File("myfile.txt");
Scanner sc = new Scanner(file);
Pattern questionPattern = Pattern.compile("(^(.+\\?)(?=(\\s+(a\\.\\s+.+)"
        + "\\s+b\\.\\s+.+\\s+c\\.\\s+.+\\s+d\\.\\s+.+)))", Pattern.CASE_INSENSITIVE);
Pattern optionAPattern = Pattern.compile("((?<=(.+\\?\\s))(a\\..+)(?=(\\sb\\..+$)))");
Pattern optionBPattern = Pattern.compile("((?<=(\\s))(b\\..+)(?=(\\sc\\..+$)))");
Pattern optionCPattern = Pattern.compile("((?<=(\\s))(c\\..+)(?=(\\sd\\..+$)))");
Pattern optionDPattern = Pattern.compile("((?<=(\\s))(d\\..+)(?=(\\s*$)))");

if (sc.hasNextLine()) {
    String line = sc.nextLine();
    Matcher question = questionPattern.matcher(line);
    Matcher optionA = optionAPattern.matcher(line);
    Matcher optionB = optionBPattern.matcher(line);
    Matcher optionC = optionCPattern.matcher(line);
    Matcher optionD = optionDPattern.matcher(line);

    if(question.find()) System.out.println(question.group());
    if(optionA.find())  System.out.println(optionA.group());
    if(optionB.find())  System.out.println(optionB.group());
    if(optionC.find())  System.out.println(optionC.group());
    if(optionD.find())  System.out.println(optionD.group());
}

Output :

what's your name?
a. danny
b. pedro
c. jose
d. mikey

I think you are a beginner. Try learning regular expression to understand the code

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read specific data from a .txt file JAVA

From Dev

Reading a specific portion of the txt file using java

From Dev

read specific line from txt file java

From Dev

How to search for specific strings and integers in a .txt file C and use them?

From Dev

In python, how can I search a specific column for a specific value in a txt file then return the specific value's row?

From Dev

Search a directory for a .txt file, without needing full pathname. - Java

From Dev

Search a directory for a .txt file, without needing full pathname. - Java

From Dev

Optimize string search in .txt file

From Dev

How to search a txt file for regex?

From Dev

Java reading and splitting sentences from a text file

From Dev

How can I write to a specific line number in a txt file in Java

From Dev

Inserting a text at specific line number in a (.txt) file using java

From Dev

Copying Specific Lines To a .txt File

From Dev

Specific text in txt file in array

From Dev

Replacing a specific charcater in a .txt file

From Dev

write to txt file java

From Dev

Java parse .txt file

From Dev

Search for Specific text in a File and Copy the Following Content in JAVA

From Dev

Search for specific file name in directory

From Dev

How to search for a specific file with batch

From Dev

Search file for specific string and remove it

From Dev

Search specific file in external storage

From Dev

How to extract specific lines from large .txt file based on search pattern

From Dev

Command for specific search in a file and dump into other file

From Dev

VIM Search - FuzzyFinder to search specific file

From Dev

BufferedReader - Search for string inside .txt file

From Dev

Search for file end with .txt in subdirectories in linux

From Dev

python, search .txt file and inject character

From Dev

Flag controlled while loop to search txt file

Related Related

  1. 1

    Read specific data from a .txt file JAVA

  2. 2

    Reading a specific portion of the txt file using java

  3. 3

    read specific line from txt file java

  4. 4

    How to search for specific strings and integers in a .txt file C and use them?

  5. 5

    In python, how can I search a specific column for a specific value in a txt file then return the specific value's row?

  6. 6

    Search a directory for a .txt file, without needing full pathname. - Java

  7. 7

    Search a directory for a .txt file, without needing full pathname. - Java

  8. 8

    Optimize string search in .txt file

  9. 9

    How to search a txt file for regex?

  10. 10

    Java reading and splitting sentences from a text file

  11. 11

    How can I write to a specific line number in a txt file in Java

  12. 12

    Inserting a text at specific line number in a (.txt) file using java

  13. 13

    Copying Specific Lines To a .txt File

  14. 14

    Specific text in txt file in array

  15. 15

    Replacing a specific charcater in a .txt file

  16. 16

    write to txt file java

  17. 17

    Java parse .txt file

  18. 18

    Search for Specific text in a File and Copy the Following Content in JAVA

  19. 19

    Search for specific file name in directory

  20. 20

    How to search for a specific file with batch

  21. 21

    Search file for specific string and remove it

  22. 22

    Search specific file in external storage

  23. 23

    How to extract specific lines from large .txt file based on search pattern

  24. 24

    Command for specific search in a file and dump into other file

  25. 25

    VIM Search - FuzzyFinder to search specific file

  26. 26

    BufferedReader - Search for string inside .txt file

  27. 27

    Search for file end with .txt in subdirectories in linux

  28. 28

    python, search .txt file and inject character

  29. 29

    Flag controlled while loop to search txt file

HotTag

Archive