2014-06-26 37 views
0

我想知道如何将以下数据拆分为多个列表。这里是我的输入(来自文本文件,此处重新创建示例):将ArrayLists拆分为多个ArrayLists

aaaa bbbb cccc,ccc,cccc 

aaaa-- bbbb 

aaaa bbbb cccc- 

aaaa bbbb cccc,ccc 

aaaa- 

aaaa bbbb ccc,cccc,cccc 

分隔每段文本都是空白。我需要编写的代码应该创建三个列表,由每行文本文件中每个条目的a,b和c组组成,而忽略任何带有“ - ”的行。因此,如下我3列应填写:

Array1: aaaa, aaaa, aaaa 
Array2: bbbb, bbbb, bbbb 
Array3: (cccc,ccc,cccc),(cccc,ccc),(ccc,cccc,cccc) 

括号里加入表明,第三阵应包括所有列出的C值 A,B和C都含有从文本文件导入的字符串。这是我的代码到目前为止:

import java.util.*; 
import java.io.*; 

public class SEED{ 

public static void main (String [] args){ 

    try{ 

     BufferedReader in = new BufferedReader(new FileReader("Curated.txt")); 
     String temp; 
     String dash = "-"; 
     int x = 0; 
     List<String> list = new ArrayList<String>(); 
     List<String> names = new ArrayList<String>(); 
     List<String> syn = new ArrayList<String>(); 
     List<String> id = new ArrayList<String>(); 


     while((temp = in.readLine()) != null){ 

      if(!(temp.contains(dash))){ 

       list.add(temp); 

       if(temp.contains(" ")){ 

        String [] temp2 = temp.split(" "); 
        names.add(temp2[0]); 
        syn.add(temp2[1]); 
        id.add(temp2[2]); 

       }else{ 

        System.out.println(temp); 

       }//Close if 

       System.out.println(names.get(x)); 
       System.out.println(syn.get(x)); 
       System.out.println(id.get(x)); 

      x++; 


      }//Close if 


     }//Close while 

    }catch (Exception e){ 

e.printStackTrace(); 
     System.exit(99); 

    }//Close try 

}//Close main 

}//Close class 

但我的输出总是:什么都没有。我怎样才能将这些值正确保存到3个独立的数组或列表中?

+0

好,我会遍历所有元素我会用一个lo t的if和string的contains()来做到这一点。 – Leo

+0

谢谢桑托斯先生,我能找到以下错误: java.lang.IndexOutOfBoundsException:指标:1,尺寸:1 \t在java.util.ArrayList.rangeCheck(ArrayList.java:604) \t在SEED.main(SEED.java:39) – user3781288

+0

处,java.util.ArrayList.get(ArrayList.java:382) \t我不会在你的异常处理中使用System.exit(99) – Leo

回答

0

可能是一个例外,并且你正在忽略它。

你抓更改为

}catch (Exception e){ 
    e.printStackTrace(); 
    System.exit(99); 

}//Close try 
+0

我得到indexoutofbound异常。但是,为什么 - 当我向每个列表添加更多值时,我的3个列表是否会变得更大?在代码 – user3781288

1

你引用list.get(X),但你的X ++和你list.add不会在同步,如果你读了没有短划线。所以(x)将不会是正确的参考。

你为什么这样做:

String [] temp2 = list.get(x).split(" "); 

相反的:

String [] temp2 = temp.split(" "); 

编辑

尝试:

if(!(temp.contains(dash))){ 

      list.add(temp); 

      if(temp.contains(" ")){ 

       String [] temp2 = temp.split(" "); 
       names.add(temp2[0]); 
       syn.add(temp2[1]); 
       id.add(temp2[2]); 
      }else{ 

       System.out.println(temp); 

      }//Close if 

     }//Close if 

for(int x = 0; x < names.size(); x++) { 

      System.out.println(names.get(x)); 
      System.out.println(syn.get(x)); 
      System.out.println(id.get(x)); 
} 
+0

的第39行的索引1,大小1处抛出异常哦,这会使它稍微简单一些。老实说,当我编写代码时,我只是想用列表的方式来思考。但是不是我的** x ++ **在检查短划线的if语句之外,因此不应该通过while循环更新吗? – user3781288

+0

如果你用破折号读一行,你不会将它添加到列表中,但是你仍然在递增x。所以,当你引用list.get(x)时,你会得到一个indexoutofbounds异常 –

+0

这很有道理,但是当我尝试移动** x ++ **语句时,它只会在未检测到短划线时递增,发生了错误。对不起,如果这些看起来像愚蠢的问题,它已经多年,因为我碰到CS,我有点生锈 – user3781288