2012-08-23 41 views
0

我已经读取了一个文件到一个数组中,但是想知道如何解析该数组中的某些值。Java Array解析

我的代码:

 ... 
     try{ 
      ... 
      String strLine; 
      String delims= "[ ]+"; 
      //parsing it 
      ArrayList parsedit = new ArrayList(); 
      while((strLine=br.readLine())!=null){ 
       System.out.println("Being read:"); 
       System.out.println(strLine); 
       parsedit.add(strLine.split(delims)); 
      } 
      System.out.println("Length:" + parsedit.size()); 
      in.close(); 
      ... 

,我读的文件是这样的:

a b c d e f g 
1 2 3 4 5 6 7 
1 4 5 6 3 5 7 
1 4 6 7 3 2 5 

这使得像这样的输出:

How many files will be input? 1 
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc 
Being read: 
a b c d e f g 
Being read: 
1 2 3 4 5 6 7 
... 
Length:4 

我想解析出这些数据,并且只剩下第一个和第五个值,以便它可以像这样读取:

a e 
1 5 

有没有人有关于如何去做的建议? 编辑: 继一些我已经改变了我的代码的建议:

public static void main(String[] args) { 
     System.out.print("How many files will be input? "); 
     Scanner readIn=new Scanner(System.in); 
     int input=readIn.nextInt(); 
     int i=1; 
     while(i<=input){ 
      System.out.println("Hey, please write the full path of the input file number" + i + "! "); 
      Scanner fIn=new Scanner(System.in); 
      String fileinput=fIn.nextLine(); 
      try{ 
      FileInputStream fstream=new FileInputStream(fileinput); 
      DataInputStream in=new DataInputStream(fstream); 
      BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
      String strLine; 
      String delims= "[ ]+"; 
      ArrayList parsedit = new ArrayList(); 
      while((strLine=br.readLine())!=null){ 
       System.out.println("Being read:"); 
       System.out.println(strLine); 
       parsedit.add(strLine.split(delims)); 
      } 
      String[] splits=strLine.split(delims); 
      System.out.println("Length:" + splits.length); 
      in.close(); 
     } 
     catch(Exception e){ 
      System.err.println("Error: " + e.getMessage()); 
     } 
     i++; 
    } 
    } 
} 

这不给回任何错误,但它似乎并没有被工作都是一样的。我错过了一些愚蠢的东西吗?输出是这样的:

How many files will be input? 1 
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc 
Being read: 
a b c d e f g 
Being read: 
1 2 3 4 5 6 7 
... 

但尽管我有一条线,告诉我数组的长度,它永远不会被打印出来,这告诉我,我刚刚结束了打破超过我固定的。你知道我可能会遗漏/忘记什么吗?

+0

除了'BufferedReader.readLine()',你可以尝试使用'BufferedReader.read()'逐个字符地读取字符以找到你要找的东西。 – Shaded

+0

请不要使用DataInputStream来读取文本http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html它比有用的更混乱。 –

+0

@PeterLawrey你会推荐什么? – Stephopolis

回答

1

split()函数返回一个字符串数组,表示从原始String获取的令牌。

所以,你想要的是仅保持第一和第五代币(拆分[0] &分割[4]):

String[] splits = strLine.split(delims); 
    //use the splits[0] & splits[4] to create the Strings you want 

关于你提到的更新,替换这样的:

while((strLine=br.readLine())!=null){ 
      System.out.println("Being read:"); 
      System.out.println(strLine); 
      parsedit.add(strLine.split(delims)); 
} 

有了这个:

while((strLine=br.readLine())!=null){ 
      System.out.println("Being read:"); 
      String splits[] = strLine.split(delims); 
      System.out.println(splits[0]+" "+splits[4]); 
      parsedit.add(splits); 
} 
+0

@keppil:你为什么要删除代码? – Razvan

+0

我把你的'splits [5]'改成了'splits [4]',显然摧毁了你的并发编辑。抱歉。 – Keppil

+0

这会在“parsedit.add(strLine.split(delims));”线? – Stephopolis

0

你可以试试这个

String[] line = s.split("\\s+"); 

这样的元素都将被存储在一个序列中的阵列line英寸 这将允许您访问任何你想要的元素。

+0

你是否建议这样做,而不是:parsedit.add(strLine.split(delims)); ? – Stephopolis

+0

我的建议是将整个页面读入字符串。然后将字符串的字符分割成数组。 –

0

你只需要获得第一和第五值了的由strLine.split(delims)返回的。我假设你知道你在做什么,你现在的代码是假定每一行将包含相同数量的“列”,至少由1个空格字符分隔,这是一个公平的假设,不管。)

+0

但是,您如何推荐获取第一个和第五个值? – Stephopolis

+0

@User:'split()'函数返回一个数组,你可以得到第一个和第五个值@索引0和4。 – nhahtdh