将文档中的单词后面出现的字符串存储到数组中并将它们组合在一起

时间:2014-03-24 11:37:56

标签: java arrays parsing text-files

我有一个文件,除其他数据外,还有以下几行:

group = A_1
group_sub = A101,A102,A103,A104
group = A_2
group_sub = A201,A202,A203,A204,A205,A206

A1是男性,A2是女性。 我需要读入文件,并且每次到达单词" group"时,将组的名称(例如,A_1)存储在数组中。 然后转到下一行并将组中的主题存储为另一个阵列[A101,A102,A103,A104]。 然后我需要将该组的名称与该组中的科目合并为男性和女性,如下所示:

[A_1,A101,A102,A103,A104] (for males)
[A_2,A201,A202,A203,A204,A205,A206] (for females)

我的代码:

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);
}
}

目前,它从循环中打印出来:

[A_1]
[A101,A102,A103,A104]
[A_2]
[A201,A202,A203,A204,A205,A206]

当我尝试合并数组时,它们返回空。我做错了什么?

3 个答案:

答案 0 :(得分:0)

在您的第一次arraycopy()来电时,您要将要复制的组件数量指定为groupSubjects.length,但您希望将groupTitle数组复制到数组both

所以如果你改变了

System.arraycopy(groupTitle, 0, both, 0, groupSubjects.length);

System.arraycopy(groupTitle, 0, both, 0, groupTitle.length);

首先将groupTitle数组添加到both数组。之后,您的第二个语句会将groupSubjects数组附加到both数组,并且您的数组将被合并。

答案 1 :(得分:0)

尝试使用此代码我们需要先执行group而不是group_sub,因此我们必须在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));
                       }
                }


}

这将给出像这样的输出

[A_1, A101,A102,A103,A104]
[A_2, A201,A202,A203,A204,A205,A206]

答案 2 :(得分:0)

试试此代码

public class Test {
    File fromFile = new File("Data.txt");         
BufferedReader br = new BufferedReader(new FileReader(fromFile));
for ( String line = br.readLine() ; line !=null;line=br.readLine())
{
    String[]groupTitle = null;
    String[]groupSubjects = null;
    System.out.println ( line );
    if (line.startsWith("group"))
    {  
        String[] title = line.split("= ");                
        groupTitle= title[1].split(" ");
      //  System.out.println(Arrays.toString(groupTitle));
    } 
    line = br.readLine();
    if (line.startsWith("group_sub"))
    {
            System.out.println ( line );
            String[] names = line.split("= ");
             System.out.println ( names.length );
             groupSubjects= names[1].split(", "); 
//             System.out.println(Arrays.toString(groupSubjects));
    }
        String[] both = new String[groupTitle.length + groupSubjects.length];
        System.arraycopy(groupTitle, 0, both, 0, groupTitle.length);
        System.arraycopy(groupSubjects, 0, both, groupTitle.length, groupSubjects.length);
       System.out.println(Arrays.toString(both));
}
}