How to handle unexpected blank space when splitting String by blank space in Java

Bryan :

I am ingesting a file line by line and each line has a varying number of space delimited strings as seen below. This is a single row from the file. The code works as expected except for the case of this line where there is an unexpected space like in El Paso here: EL PASO*TX*K4. I'd use defined indexes for each string if there were a fixed number of strings per row but there are not. Some rows will have as few as 7 strings where others have as many as 21 strings to ingest. Any one have a good idea on how to best handle this situation?

NAV3ELP VORTAC ALIBY*NM*K2 ANJIE*TX*K4 CINAG*TX*K4 EL PASO*TX*K4 FABAN*TX*K4 FANNY*TX*K4 FARCY*TX*K4 FEMOL*TX*K4 FIGMO*TX*K4 GIFEN*TX*K4 GREBE*NM*K2 HANCH*NM*K2 JIDEN*TX*K4 KEYLO*TX*K4 LINZY*TX*K4 NOCIG*TX*K4 NUQUH*TX*K4 PIERS*TX*K4 RIOWE*TX*K4 RISGE*TX*K4 RUTER*NM*K2

    public Nav3Data parseRecord(String dataLine) {
        // check record type
        if(!dataLine.startsWith("NAV3")) return null;

        Nav3Data theNav = new Nav3Data();
        // parse Navaid ID
        theNav.setNavaidID(dataLine.substring(NAVAID_ID_START_IDX, NAVAID_ID_END_IDX).trim());
        // parse fixes
        String[] fixes = dataLine.substring(FIX_IDS_START_IDX, FIX_IDS_END_IDX).split("\\s+"); 
        theNav.setFixIDs(fixes);
        return theNav;
    }
diginoise :

Split on 2 spaces or more:

dataLine.substring(FIX_IDS_START_IDX, FIX_IDS_END_IDX).split("\\s{2,}")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23566&siteId=1