2013-11-26 33 views
-1
Scanner input = new Scanner(new FileReader(selectFile.getSelectedFile())); 
int count = 0; 
System.out.println("SampleName,RecordingTime,PeakName,Amount"); 
while(input.hasNext() && count <= 1000){ 
    count++; 
    String word = input.next(); 

    if (count == 2){ 
     System.out.print(word + ","); 
    } 
    if (count == 36){ 
     System.out.print(word + ","); 
    } 

    if (count == 64){ 
     System.out.print(word + ","); 
    } 

    if (count == 68){ 
     System.out.print(word + ","); 
    } 

} 

如何打印序列中的下一個單詞?例如,程序讀取一個如下所示的文本文件。打印序列中的下一個單詞

你好

名稱:約翰 金額:12.0

我怎麼會去名稱後打印的字:在這種情況下,約翰和金額:12.0 在此先感謝。

+0

什麼是計數的2,36,64和68值? – Adarsh

+0

這與例子幾乎一樣。我想要打印的文字,但由於並非所有文件都具有相同數量的文字,我所做的代碼並不是我所需要的。 – yamp03

+0

但是,該標籤被發現的索引的計數? – Adarsh

回答

0

這裏有一種方法可以做到這一點。

while (input.hasNext()) { 
     String word = input.next(); 
     Pattern pattern1 = Pattern.compile("Name:"); 
     Pattern pattern2 = Pattern.compile("Amount:");    
     Matcher matcher1 = pattern1.matcher(word); 
     Matcher matcher2 = pattern2.matcher(word); 
     if(matcher1.matches()){ 
      System.out.println(input.next()); 
     } 
     if(matcher2.matches()){ 
      System.out.println(input.next()); 
     }   
    } 
相關問題