2015-04-24 125 views
5

我必須編寫一個程序來解析棒球運動員的信息,並從txt文件中點擊,輸出,漫步等。例如,txt文件可能看起來像這樣: Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s, h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o解析txt文件

我知道如何解析這個文件並且可以讓代碼運行完美。我遇到的唯一問題是我們應該只打印每個玩家的名字和3或他們的劇本。例如: 山姆猛男打,打,出 吉爾詹克斯出來,出來,高飛犧牲 威爾·瓊斯出來,出來,步行

我不知道如何這一點,每次我試圖把它割下的限制3我總是讓第一個人工作得很好,但是它打破了循環,對所有其他球員都沒有做任何事情。

這是我到目前爲止有:

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

public class ReadBaseBall{ 

public static void main(String args[]) throws IOException{ 

    int count=0; 
    String playerData; 
    Scanner fileScan, urlScan; 

    String fileName = "C:\\Users\\Crust\\Documents\\java\\TeamStats.txt"; 
    fileScan = new Scanner(new File(fileName)); 

    while(fileScan.hasNext()){ 

     playerData = fileScan.nextLine(); 
     fileScan.useDelimiter(","); 

     //System.out.println("Name: " + playerData); 

     urlScan = new Scanner(playerData); 
     urlScan.useDelimiter(","); 


     for(urlScan.hasNext(); count<4; count++) 

      System.out.print(" " + urlScan.next() + ","); 

     System.out.println(); 

     } 
    } 
} 

此打印出: 山姆猛男,H,H,O, 但隨後其他球員都作廢了。我需要幫助以獲得其他打印。

+0

您是否有文件格式控制權? –

+0

可悲的是。該文件應該有可能是無限的。文件中可能有許多玩家。 – cweidner55

+0

您需要重置您的'count'變量,請看下面的答案。 – Phoenix

回答

0

第一個問題

變化while(fileScan.hasNext()))while(fileScan.hasNextLine())。這不是一個突破性的問題,但使用掃描儀時,通常會在sc.has*後面加上sc.*

第二問題

卸下線fileScan.useDelimiter(",")。這一行在這種情況下不會執行任何操作,而是替換默認的分隔符,以便掃描器不再以空格分隔。使用Scanner.nextLine時無關緊要,但稍後會出現一些令人討厭的副作用。

第三個問題

改變這一行for(urlScan.hasNext(); count<4; count++)while(urlScan.hasNext())。老實說,我很驚訝這行甚至編譯,如果它只讀了掃描儀的前4個。

如果你希望限制每行處理量可以用

for(int count = 0; count < limit && urlScan.hasNext(); count++)

這取代它會限制讀limit同時還處理具有比限制更少的數據線的量。

確保您的每個數據集都由一行分隔,否則輸出可能沒有太大意義。

0

您需要將count車重while循環:

while(fileScan.hasNext()){ 
     count = 0; 
     ... 
} 
0

你不應該對此多臺掃描儀 - 假設你在你的問題貼,你可以使用正則表達式來做到這一點的格式。

這演示了一個正則表達式來匹配播放器並用作掃描器的分隔符。我在我的例子中給掃描器提供了一個字符串,但是不管來源如何,這種技術都是一樣的。

int count = 0; 
Pattern playerPattern = Pattern.compile("\\w+\\s\\w+(?:,\\w){1,3}"); 
Scanner fileScan = new Scanner("Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s,h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o"); 
fileScan.useDelimiter("(?<=,\\w)\\s"); 

while (fileScan.hasNext()){ 
    String player = fileScan.next(); 
    Matcher m = playerPattern.matcher(player); 
    if (m.find()) { 
     player = m.group(0); 
    } else { 
     throw new InputMismatchException("Players data not in expected format on string: " + player); 
    } 
    System.out.println(player); 
    count++; 
} 
System.out.printf("%d players found.", count); 

輸出:

Sam Slugger,h,h,o 
Jill Jenks,o,o,s 
Will Jones,o,o,w 

Scanner.delimiter()的通話設置要用於檢索令牌的分隔符。正則表達式(?<=,\\w)\\s

(?<        // positive lookbehind 
    ,\w       // literal comma, word character 
) 
\s        // whitespace character 

其限制球員通過他們的作品之間的空間沒有匹配任何東西,但這個空間,和不匹配的名稱之間的空間。

用於提取高達每播放器3次正則表達式是\\w+\\s\\w+(?:,\\w){1,3}

\w+        // matches one to unlimited word characters 
(?:        // begin non-capturing group 
    ,\w       // literal comma, word character 
){1,3}       // match non-capturing group 1 - 3 times 
+0

謝謝你們,這些答案真的幫助了很多,現在我得到了它的工作。 – cweidner55

+0

@ cweidner55如果此答案解決了您的問題,請點擊綠色支票。 – Voldemort

1

在這裏,用的FileReader 嘗試假設你的文件內容格式是這樣

Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s 
Jill Johns,h,h,o,s,w,w,h,w,o,o,o,h,s 

與此一每個球員在他/她自己的線路然後這可以爲你工作

BufferedReader reader; 
    try { 
     reader = new BufferedReader(new FileReader(new File("file.txt"))); 
     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      String[] values_per_line = line.split(","); 
      System.out.println("Name:" + values_per_line[0] + " " 
        + values_per_line[1] + " " + values_per_line[2] + " " 
        + values_per_line[3]); 
      line = reader.readLine(); 
     } 
     reader.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

否則,如果它們全部排成一行,那麼這將不合理,然後修改此示例。

Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s| John Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s 

BufferedReader reader; 
    try { 
     reader = new BufferedReader(new FileReader(new File("file.txt"))); 
     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      // token identifier is a space 
      String[] data = line.trim().split("|"); 
      for (int i = 0; i < data.length; i++) 
       System.out.println("Name:" + data[0].split(",")[0] + " " 
         + data[1].split(",")[1] + " " 
         + data[2].split(",")[2] + " " 
         + data[3].split(",")[3]); 
      line = reader.readLine(); 
     } 
     reader.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }