D J
D J

Reputation: 57

How do I read 2 particular columns in Java from a txt file?

How do I read 2 particular column (1st column and 3rd Column) from a .txt file.The columns are separated by different delimiters (I want to ignore the 2nd column in which (base 16) is written). Also how do I skip the column headings. The .txt file looks as follows:

IOU/AB-L                                                    Organization                                 
company_id                                                  Organization                                 
                                                            Address                                      

D0-AB-DB   (hex)             Ahenhen ViewAt Technology Co.,Ltd. 
D0ABDB     (base 16)         Ahenhen ViewAt Technology Co.,Ltd. 
                             9A,Microprofit,6th Gaoxin South Road, High-Tech 
                             Industrial Park, Nanshan, henzhen.
                             henzhen  guangdong  51867
                             DN

42-05-F5   (hex)            Integrated Technology (Malaysia) Sdn. Bhd.
4205F5     (base 16)        Integrated Technology (Malaysia) Sdn. Bhd.
                            Phase 1, Bayan Aepas FIZ
                            Bayan Lepas  Penang  11923
                            NY

The code which I am trying is:

String line;
BufferedReader reader = new BufferedReader(new FileReader(path));
while ((line = reader.readLine()) != null)
{
    String[] parts = line.split("   ", 3);
    if (parts.length >= 3)
    {
        String key = parts[0];
        String value = parts[2];
        System.out.println("Key value pair is "+key+"   "+value);
    } 
}

So basically I want to read D0-AB-DB and Ahenhen ViewAt Technology Co.,Ltd. in the 1st line and then 42-05-F5and Integrated Technology (Malaysia) Sdn. Bhd. in the 2nd line.

Can someone please suggest some edit?
What regular expression should I use in this case?

Thanks in advance!

Upvotes: 0

Views: 936

Answers (2)

DJDaveMark
DJDaveMark

Reputation: 2855

They key is to split on at least 2 spaces \s{2,}, use a split limit of 4, and use booleans to know if you've gone passed the header, or are ignoring the detail lines:

boolean passedHeader = false;
boolean skipDetail = false;

String line;
BufferedReader reader = new BufferedReader(new FileReader(path));

while ((line = reader.readLine()) != null) {
    line = line.trim();

    if (!passedHeader) { // skip header
        if (line.isEmpty()) {
            passedHeader = true;
        }
        continue;
    }

    if (skipDetail) { // skip detail
        if (line.isEmpty()) {
            skipDetail = false;
        }
        continue;
    }

    if (line.isEmpty()) { // skip empty lines
        continue;
    }

    String[] parts = line.split("\\s{2,}", 4);

    if (parts.length >= 3) {
        String key = parts[0];
        String value = parts[2];
        System.out.println("Key: \"" + key + "\"   Value: \"" + value + "\"");
    } else {
        System.out.println("Encountered the following line of unexpected format:");
        System.out.println(line);
    }

    skipDetail = true;
}

Notes:

I reformatted the console output adding double quotes to show everything clearly.

I added an else for when the format of the line is unexpected, which would happen of any line happened to be something like this D0-AB-DB (hex) (only one space between D0-AB-DB and (hex))

Here's what would happen with a split limit of 3 and an input line of 4 columns or more. All the extra columns are included in the 3rd column's text. i.e. with the following:

D0-AB-DB   (hex)             Ahenhen ViewAt Technology Co.,Ltd.       Column 4 text

you'd get:

Key: "D0-AB-DB"   Value: "Ahenhen ViewAt Technology Co.,Ltd.       Column 4 text"

Upvotes: 0

gkhaos
gkhaos

Reputation: 705

You can use "\s+" instead of " " in your split method to slit the string by whitespaces

I'm pretty sure here you will find what you want: https://docs.oracle.com/javase/8/docs/api/index.html?java/util/StringTokenizer.html

Upvotes: 1

Related Questions