Lightvvind
Lightvvind

Reputation: 155

How to parse this information from a string?

country     residents   area      capital
Andorra     71201       468       Andorra la Vella
Italien     58133509    301230    Rom
San Marino  29251       61        San Marino

I need to store the information (capital, residents, area, capital) in different variables. How would I go about parsing this? Notice that sometimes there are spaces in the names.

I have tried reading each token ( scanner.next() ) this fails when there are spaces in the capital or country name.

I have tried reading each line and then parsing it but I can't figure out a way to parse everything correctly since there are sometime spaces in the names. (I used indexOf() and substring() )

This is part of a bigger file but there are no spaces in the residents or area field in the entire field.

My try:

while(scanner.hasNext()){

   String info = scanner.nextLine();
   //parse string
   int nameindex = info.indexOf(" ");
   System.out.println(info.substring(0,nameindex));
   int resindex = info.indexOf(" ", nameindex);
}

Upvotes: 0

Views: 89

Answers (1)

Faraj Farook
Faraj Farook

Reputation: 14875

I hope you have a multiline string as per your question title. So why don't you simply use a regex for the whole content. Given the string is stored in the variable data

data.split("[ ]{2,}")

This would give the array of data as a whole. So when you have to parse it you can simply do a loop 4 elements at a time

(edit)

or else you can simply use this function... hope this will be easier for you.

List<Map<String, String>> parse(String data){
    List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
    String[] lines = data.split("\n");  
    String[] keys = lines[0].split("[ ]{2,}");
    for (int i = 1; i < lines.length; i++) {
        String row[] = lines[i].split("[ ]{2,}");
        Map<String, String> rowMap = new HashMap<String, String>();
        for (int j = 0; j < row.length; j++) {
            rowMap.put(keys[j], row[j]);
        }
        dataList.add(rowMap);
    }
    return dataList;
}

Upvotes: 1

Related Questions