java - Storing strings that appear after a word in a document into arrays and combining them -
i have file, which, among other data, has following lines:
group = a_1 group_sub = a101,a102,a103,a104 group = a_2 group_sub = a201,a202,a203,a204,a205,a206
a1 males, , a2 females. need read in file, , each time reaches word "group", store name of group (for example, a_1) in array. move on next line , store subjects in group array [a101,a102,a103,a104]. , need merge name of group subjects in group males , females so:
[a_1,a101,a102,a103,a104] (for males) [a_2,a201,a202,a203,a204,a205,a206] (for females)
my code:
public class test { file fromfile = new file(filename); bufferedreader br = new bufferedreader(new filereader(fromfile)); string line; while ((line = br.readline()) != null){ string[]grouptitle = null; string[]groupsubjects = null; if (line.startswith("group")){ string[] title = line.split("= "); grouptitle= title[1].split(" "); // system.out.println(arrays.tostring(grouptitle)); } if (line.startswith("group_sub")){ string[] names = line.split("= "); groupsubjects= names[1].split(", "); // system.out.println(arrays.tostring(groupsubjects)); } string[] both = new string[grouptitle.length + groupsubjects.length]; system.arraycopy(grouptitle, 0, both, 0, groupsubjects.length); system.arraycopy(groupsubjects, 0, both, grouptitle.length, groupsubjects.length); } }
at moment, within loop prints out :
[a_1] [a101,a102,a103,a104] [a_2] [a201,a202,a203,a204,a205,a206]
when try merge arrays, return empty. doing wrong?
try code need first group
, group_sub
have merge arrays after group_sub
.
while ((line = br.readline()) != null){ if (line.startswith("group")&&line.contains(",")==false){ string[] title = line.split("= "); grouptitle= title[1].split(" "); // system.out.println(arrays.tostring(grouptitle)); } if (line.startswith("group_sub")){ string[] names = line.split("= "); groupsubjects= names[1].split(", "); // system.out.println(arrays.tostring(groupsubjects)); if(grouptitle!=null&&groupsubjects!=null){ string[] both = new string[grouptitle.length + groupsubjects.length]; system.arraycopy(grouptitle, 0, both, 0, groupsubjects.length); system.arraycopy(groupsubjects, 0, both, grouptitle.length, groupsubjects.length); system.out.println(arrays.tostring(both)); } } }
this give output this
[a_1, a101,a102,a103,a104] [a_2, a201,a202,a203,a204,a205,a206]
Comments
Post a Comment