-1

I need to read a txt file and check if the text file has "First Name". If it contains, it should split the line and display the line in the console.

public String[] getFullname() {
    try {
        BufferedReader br = new BufferedReader(new FileReader("src\\main\\resources\\CV.docx"));
        String line = br.readLine();
        while (line != null) {
            if (line.startsWith("Full Name")) {
                String[] fname = line.split(":");
                System.out.println(Arrays.toString(fname));
                firstname = fname;
            }
            line = br.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return firstname;
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Dharshan
  • 37
  • 7
  • 1
    Can you provide the sample input and problem you are facing? – Dinesh Jul 21 '18 at 08:31
  • 2
    Uh, a `docx` file is most definitely not a text file. First, you'll have to unzip it, then, find the correct file within the zip archive that contains the text, then, parse that file (xml), and then check for the text you're looking for. Good luck. – David Conrad Jul 21 '18 at 09:02
  • Also, you haven't actually asked a question. You say that you "need to read a `txt` file and check &c." Okay, you need to do that. What's the problem? – David Conrad Jul 21 '18 at 09:03
  • Also also, why are you catching `FileNotFoundException` *and* `IOException`? An `IOException` *is a* `FileNotFoundException` – David Conrad Jul 21 '18 at 09:04
  • Thanks @DavidConrad – Dharshan Jul 24 '18 at 05:48

3 Answers3

1
  1. You are comparing First Name with Full Name. That's why the if condition fails.

    Use

    if (line.startsWith("First Name"))
    

    Instead of

    if (line.startsWith("Full Name")) 
    
  2. Use contains instead of startsWith if you want to check if the line has the word First Name. 'startsWith' only checks if your line's starting characters match with your string.

    if(line.contains("First Name")){
     // your logic 
    }
    
malware
  • 393
  • 1
  • 3
  • 16
1

It is due to docx file. There is apache poi API which is used for reading files written using MS Office suite products. I have tried it once for excel files. The way you are doing it, is valid for text files.

First you can check logging content of file on console. If you are able then only you can check it starts with something.

Please refer to the answer posted here for reading docx files:

https://stackoverflow.com/a/34543577/4940369

Please also correct "Full Name" with "First Name".

Logic used seems incorrect as well:

split() method returns array of strings separated by character/string delimiter. firstName is not declared anywhere in this block. You are trying to assign array to String. You can use substring(line.indexOf(':')) something.

It will always return the name found in last line which starts with "First Name". There is no break or something. firstName variable will be reassigned each time when line starts with 'First Name'. You can use list instead of Array, if want to return each first name. Keep adding them in list as they found.

Abhishek Tiwari
  • 332
  • 2
  • 16
0

...check if the text file has "First Name"...

In your code you are searching for Full Name instead of First Name.

Replace

if (line.startsWith("Full Name")) {

with

if (line.startsWith("First Name")) {

UPDATE:

As David has mentioned in this comment, if you are trying to read a Word document. Your attempt is completely useless.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80