2012-08-23 42 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