1

I've a text file with the following matrix/table:

# Matrix size is 13x13
        DEPOT   CHESHNT LIVST   CHINGFD ENFLDTN HERTFDE BSHPSFD CAMBDGE STFD    ELYY    KLYNN   STANAIR BROXBRN
DEPOT   00:00   01:00   00:28   00:02   00:40   01:00   00:02   00:05   01:00   00:30   01:00   00:31   01:00   
CHESHNT 01:00   00:00   00:38   01:00   01:03   01:27   01:02   01:05   01:56   01:21   02:00   01:25   02:00   
LIVST   00:28   00:38   00:00   00:22   00:25   00:49   01:00   01:27   01:20   01:44   01:24   00:47   01:24   
CHINGFD 00:02   01:00   00:20   00:00   00:42   01:02   00:45   01:30   00:58   01:45   01:30   00:45   01:02   
ENFLDTN 00:40   01:03   00:25   00:42   00:00   01:14   00:42   00:45   01:36   01:01   01:40   01:11   01:40   
HERTFDE 01:00   01:27   00:49   01:02   01:14   00:00   01:02   01:05   00:56   01:21   02:00   01:31   01:26   
BSHPSFD 00:02   01:02   01:00   01:00   00:42   01:02   00:00   00:30   00:54   00:47   01:02   00:33   01:02   
CAMBDGE 00:03   01:05   01:27   01:30   00:45   01:05   00:30   00:00   01:01   00:17   01:05   00:36   01:05   
STFD    01:00   01:56   01:20   00:58   01:36   00:52   00:54   01:01   00:00   01:17   01:56   01:06   00:30   
ELYY    00:22   01:30   01:44   01:45   01:10   01:30   00:47   00:17   01:26   00:00   01:30   01:01   01:30   
KLYNN   00:52   02:00   01:24   01:30   01:40   02:00   01:02   01:05   01:56   01:21   00:00   01:31   02:00   
STANAIR 00:31   01:12   00:36   00:45   00:52   01:12   00:33   00:36   01:02   00:53   01:12   00:00   01:12   
BROXBRN 01:00   02:00   01:24   01:02   01:40   01:23   01:02   01:05   00:31   01:21   02:00   01:31   00:00   

Given that the structure is pretty common, is there no Java library to parse into a Java structure? Something like:

Map<String,String> matrix = MatrixParser.parse("matrix.txt"); //where the key is the concatenation of the row and column coordinates. 

I can see parsing it as a CSV with OpenCSV, the separator being a space or tab instead of the comma. Nothing higher level?

2
  • There's really no reason for a "library" when String.split() pretty much covers it. Commented Nov 14, 2011 at 18:11
  • Once you've parsed that with OpenCSV, you'll find turning it into the data structure you're after is really trivially easy. Probably no more than a couple dozen lines of codes, tops. Commented Nov 14, 2011 at 18:12

1 Answer 1

1

This is covered in the opencsv FAQ.

From the FAQ:

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(YourOrderBean.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);

CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, yourReader);

That said, it isn't that difficult anyway :)

You could do the same thing in a few lines of code using Commons BeanUtils' populate() method after splitting, line-by-line. Pseudo-code:

cols = loadColumnNamesToArray();
while (moreLines()) {
    vals = line.split
    vals.each_with_index {
        colName = cols[index]
        valMap.put(colName, val)
    }
    bean = Bean.new
    BeanUtils.populate(bean, valMap)
    beanList.add(bean)
}
Sign up to request clarification or add additional context in comments.

2 Comments

you see that's different from the map I'm looking for. I don't have (and don't want) a Bean.
@simpatico Use the code w/o the bean part then. And this is less common than you think; never done it in 12 years of Java business/scientific apps. Maps of strings aren't all that useful, I almost always need actual data structures in order to manipulate them more easily, in type-safe ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.