2013-05-30 51 views
1

我剛纔的問題:Java - Importing text file into array when lines are not consistent.split和.indexOf在String []數組

每次我試圖努力.split或.indexOf我相處的線一條錯誤消息:「無法調用分裂(字符串, int)在數組類型String []'上。 Eclipse是不是太大的幫助,建議我改變它。長度

我的代碼:

import java.util.*; 
import java.io.*; 
public class Club 
{ 
Scanner ConsoleInput; 
public int count; 
public Club() throws IOException 
{ 
    String clubtxt = ("NRLclubs.txt"); 
    int i; 

    File clubfile = new File(clubtxt); 

    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(); 
     SplitClubdata(teamclub, count); 
    } 
    else 
    { 
     System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n"); 
    } 

} 
public void SplitClubdata(String[] teamclub, int count) 
{ 
    String[] line = teamclub; 
    int maxlines = count; 
    count = 0; 

      while(count <= maxlines) 
      { 
      // Split on commas but only make three elements 
      String elements[] = line.split(",", 3); 


      String names[] = new String[maxlines]; 
      String mascot[] = new String[maxlines]; 
      String aliases[] = new String[maxlines]; 
      // The first belongs to names 
      names[count] = elements[0]; 
      // The second belongs to mascot 
      mascot[count] = elements[1]; 
      // And the last belongs to aliases 
      aliases[count] = elements[2]; 
      count++; 
      } 
} 
} 

任何人有關於如何解決這一問題的任何想法?

+1

你有沒有考慮使用'List'([ArrayList的(http://docs.oracle.com/javase/6/docs/api/java /util/ArrayList.html)總是一個很好的選擇)讀取行?這將阻止你不得不讀取文件兩次(一次數,再次存儲)。 – Supericy

+0

Eclipse很明顯「不能在數組類型String []上調用split(String,int)」,這意味着你的'line'是一個字符串數組,並且他只期望一個字符串。 – TecHunter

回答

0

拆分在字符串對象上調用,而不是在字符串數組上調用。

Probabaly這是你應該做的:

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

,而不是

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

您試圖split()數組,你只能叫上String這些方法:

while(count <= maxlines) 
{ 
    // Split on commas but only make three elements 
    String elements = line[count].split(",", 3); 
    ... 
0

方法indexOf和上不提供。

可用於陣列的字段/方法包括:

公共最終字段長度,它包含部件陣列的 的數量。長度可能是正數或零。

公共方法克隆,它覆蓋Object類中同名 的方法,並且不引發檢查的異常。返回類型爲 的數組類型T []的克隆方法是T []。

多維數組的克隆很淺,也就是說 它只創建一個新的數組。子陣是共享的。

所有從類Object繼承的成員;對象 未被繼承的唯一方法是其克隆方法。

Java Specification

1

lineString數組,你不能在它調用split。我認爲你的意思是line[count].split(",", 3)

我也建議重組這個類,並使用正確的技術:

  • 不讀文件兩次獲得count
  • 使用ArrayList<Club>其中俱樂部有字段(mascot,namealias)。

這裏是一個清潔的版本:

package org.argaus.gwt.tls.portlet; 

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

public class Club { 

    private String name; 
    private String mascot; 
    private String alias; 

    public Club(String name, String mascot, String alias) { 
     this.name = name; 
     this.mascot = mascot; 
     this.alias = alias; 
    } 

    public static List<Club> ReadClubsFromFile() throws IOException { 


     File clubfile = new File("NRLclubs.txt"); 
     List<Club> clubs = new ArrayList<Club>(); 
     if (clubfile.canRead()) { 
      Scanner inputFile = new Scanner(clubfile); 
      inputFile = new Scanner(clubfile); 
      while (inputFile.hasNext()) { 
       String[] parts = inputFile.nextLine().split(",", 3); 
       clubs.add(new Club(parts[0], parts[1], parts[2])); 

      } 
      inputFile.close(); 
     } 
     else { 
      System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n"); 
     } 
     return clubs; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getMascot() { 
     return mascot; 
    } 

    public void setMascot(String mascot) { 
     this.mascot = mascot; 
    } 

    public String getAlias() { 
     return alias; 
    } 

    public void setAlias(String alias) { 
     this.alias = alias; 
    } 
}