arraylist - Adding only specific text from a file to an array list, part 2 - Java -


i hope can have great suggestion how can solve here:

i have text file places , names - name detail indicates place(s) person has visited:

place: new york place: london place: paris place: hongkong  1. name: john 1. name detail: london 1. name detail: paris 1. name detail: hongkong  2. name: sarah 2. name detail: london  3. name: david 3. name detail: new york 3. name detail: paris 

here part of code.

private arraylist<place> places = new arraylist<>(); private arraylist<name> names = new arraylist<>();   public void load(string filename) throws filenotfoundexception {     arraylist<place> place = places;     bufferedreader br = new bufferedreader(new filereader(filename));     int namecounter = 1;     int namedetailcounter = 1;     string text;      try {         while ((text = br.readline()) != null) {                  if (text.contains("place:")) {                 text = text.replaceall("place:", "");                 places.add(new place(text));             } else if (text.contains(namecounter + ". name:")) {                 text = text.replaceall(namecounter + ". name:", "");                 names.add(new name(text, ""));                 namecounter ++;             }                 //starting here!                 else if (text.contains(namedetailcounter + ". name detail:")) {                 text = text.replaceall(namedetailcounter + ". name detail:", "");                 (name name : names) {                     name namedetails = findname(name.getname());                     place placedetails = findplace(text);                     namedetails.addname(placedetails);                   }                 namedetailcounter ++;             }         }     } catch (exception e) {         system.err.println("error: " + e.getmessage());     } } 

my idea select "1." text file first , add in array, continue "2." , add in array, , on.

i have tried many ways, did not add name detail beginning of "1." in array. appreciate new ideas or suggestions, thanks!

it might better use regular expression extract digit on line instead of trying track/guess (see below).

this tested recreation of code since reference classes didn't provide...but should help:

public static string getmatch(final string pattern, final string content) {     pattern r = pattern.compile(pattern);     matcher m = r.matcher(content);      if (m.find()) {         return m.group(1);     } else {         return "";     } }  public static void load(string filename) throws filenotfoundexception {      list<string> places = new arraylist<string>();     list<string> names = new arraylist<string>();     list<string> namedetails = new arraylist<string>();      bufferedreader br = new bufferedreader(new filereader(filename));      string text;     string lastname = "";      try {          while ((text = br.readline()) != null) {             // extract num start of line or empty if none..             string num = getmatch("^([0-9]+)\\.", text);              if (text.contains("place:")) {                 text = text.replaceall("place:", "");                 places.add(text);             } else if (text.contains(num + ". name:")) {                 text = text.replaceall(num + ". name:", "");                 names.add(text);                 lastname = text;             } else if (text.contains(num + ". name detail:")) {                 text = text.replaceall(num + ". name detail:", "");                 namedetails.add(lastname + " had " + text);             }         }     } catch (exception e) {         system.err.println("error: " + e.getmessage());     }      system.out.println("places:" + places);     system.out.println("names:" + names);     system.out.println("name details:" + namedetails); } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -