2013-05-29 17 views
0

我有一個非常有我難倒了早期的分配,剩下這是相當容易的(對數據進行排序一旦其進口,然後根據不同的重新保存名稱)。爪哇 - 導入文本文件轉換成數組時線並不一致

我們需要從一個txt文件導入數據到3個獨立的數組(名稱,吉祥物,化名),然而線條並不一致。通過不懈我的意思是一條線可能有:

Glebe,G Shield,Glebe District 

而另一條線路可能有:

St George,Knight & Dragon,Saints,Dragons,St George Illawarra 

之前的所有第一,屬於名數組。

一切第一之後,但在第二之前,屬於吉祥物陣列。

一切第二後,直到該行的末尾屬於別名陣列。

我已經能夠解決如何導入.txt文件,它包含整個行,然後我可以轉換成導入「,」和新行(使用分隔符)之前的所有內容。但是,包含超過3組數據的行會毀壞導入,因爲別名數組只能保留1而不是其他所有數據。

因此沒有人知道的,可以告訴我,幾乎做了代碼:

名=之前的所有第一,

吉祥物=第一之後的一切,但前一秒,

別名=第二之後一切,直到行

那我可以爲基地的工作進入礦井使用的結束?

經過一天的研究,我不斷想出死路一條。他們通常都涉及分割在每個逗號,但打破進口(行數多於1個別名,第二個別名被放入名稱數組,等)

這是我想出的代碼,導入整個線到一個數組:

public static void LoadData() throws IOException 
{ 
    String clubtxt = ("NRLclubs.txt"); 
    String datatxt = ("NRLdata.txt"); 
    int i, count; 

    File clubfile = new File(clubtxt); 
    File datafile = new File(datatxt); 

    if (clubfile.exists()) 
    { 
     count = 0; 
     Scanner inputFile = new Scanner(clubfile); 
     i = 0; 
     while(inputFile.hasNextLine()) 
     { 
      count++; 
      inputFile.nextLine(); 
     } 
     String [] teamclub = new String[count]; 
     inputFile.close(); 
     inputFile = new Scanner(clubfile); 
     while(inputFile.hasNext()) 
     { 
      teamclub[i] = inputFile.nextLine(); 
      System.out.println(teamclub[i]); 
      i++; 
     } 
     inputFile.close(); 
    } 
    else 
    { 
     System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n"); 
    } 

    if (datafile.exists()) 
    { 
     count = 0; 
     Scanner inputFile = new Scanner(datafile); 
     i = 0; 
     while(inputFile.hasNextLine()) 
     { 
      count++; 
      inputFile.nextLine(); 
     } 
     String [] teamdata = new String[count]; 
     inputFile.close(); 
     inputFile = new Scanner(datafile); 
     while(inputFile.hasNext()) 
     { 
      teamdata[i] = inputFile.nextLine(); 
      System.out.println(teamdata[i]); 
      i++; 
     } 
     inputFile.close(); 
    } 
    else 
    { 
     System.out.println("\n" + "The file " + datafile + " does not exist." + "\n"); 
    } 
} 
+2

的一種方式?它看起來像你正在閱讀一個文件,但沒有試圖對你得到的數據做任何事情。 – Farlan

回答

1

String.split方法與參數limit

當你在一個名爲line可變的輸入線,你可以可以調用

String[] tokens = line.split(',', 3); 

這將分裂的逗號線,同時確保它不會返回超過3個令牌。它返回一個String數組,其中第一個元素是第一個逗號前的元素,第二個元素是第一個和第二個逗號之間的元素,第三個元素是第二個逗號後面的元素。

0

可以使用String.split方法。

String line = // the line you read here 

// Split on commas but only make three elements 
String[] elements = line.split(',', 3); 

// The first belongs to names 
names[linecount] = elements[0]; 
// The second belongs to mascot 
mascot[linecount] = elements[1]; 
// And the last belongs to aliases 
aliases[linecount] = elements[2]; 
0

基本上你想要做的分割每行到你讀它在一個數組,然後解析由線數據線什麼。像這樣(僞代碼):

Scanner inputFile = new Scanner(datafile); 
while(inputFile.hasNextLine()) { 
    String line = inputFile.nextLine(); 
    String[] lineSplit = line.split(","); 
    //TODO: make sure lineSplit is at least 3 long. 
    String name = lineSplit[0]; 
    String mascot = lineSplit[1]; 

    //EDIT: Don't just get the last element, get everything after the first two. 
    // You can do this buy just getting the substring of the length of those two strings 
    // + 2 to account for commas. 
    //String alias = lineSplit[lineSplit.length() - 1]; 
    String alias = line.substring(name.length() + mascot.length() + 2); 

    //If you need to do trimming on the strings to remove extra whitespace, do that here: 
    name = name.trim(); 
    mascot = mascot.trim(); 
    alias = alias.trim(); 

    //TODO: add these into the arrays you need. 
} 

希望這有助於。

+0

是的。我會解決這個問題。 – Liron

0

嘗試尋找模式/匹配的東西 - 你需要拿出一個合適的正則表達式。

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

像這樣的東西可能會做到這一點:

static final Pattern pattern = Pattern.compile("([^,]*),([^,]*),(*$)"); 
MatchResult result = pattern.matcher(line).toMatchResult(); 
if (result.groupCount() == 3) { 
    // Found the groups 
    name = result.group(0); 
    // etc.. 
} else { 
    // failed to match line 
} 
+1

這是一個正則表達式是完全矯枉過正... –

+0

這是真的。但它是一個有用的工具。 – chessbot

1

既然你只想在第一個2個逗號解析,您可以用String split有限制。

如果您願意,可以使用字符串indexOf方法查找前2個逗號,然後使用字符串substring方法獲取逗號之間的字符。

您希望能夠使用一個逗號或者根本沒有逗號來處理一條線。

下面就來分析你做了什麼,以分手的線串線

public List<String> splitLine(String line) { 
    List<String> list = new ArrayList<String>(); 
    int firstPos = line.indexOf(","); 
    int secondPos = line.indexOf(",", firstPos + 1); 
    if (firstPos >= 0) { 
     if (secondPos >= 0) { 
      list.add(line.substring(0, firstPos)); 
      list.add(line.substring(firstPos + 1, secondPos)); 
      list.add(line.substring(secondPos + 1)); 
     } else { 
      list.add(line.substring(0, firstPos)); 
      list.add(line.substring(firstPos + 1)); 
      list.add(""); 
     } 
    } else { 
     list.add(line); 
     list.add(""); 
     list.add(""); 
    } 

    return list; 
}