2013-07-09 165 views
0

我試圖打印出數組的內容,並且在循環數組以打印元素時遇到問題。它給我的錯誤是:嘗試打印數組參數錯誤

的方法ImportTeams()在類型FILEREAD不適用於參數(INT)

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class FileRead { 

    public String[] ImportTeams(){ 

     String[] Teams; 
     BufferedReader br = null; 
     int linecount = 0; 

     try { 
      br = new BufferedReader(new FileReader("filepath")); 
     } catch (FileNotFoundException e) { 

      e.printStackTrace(); 
     } 
     try { 
      while (br.readLine() != null){ 
       linecount ++; 
      } 
      br.close(); 
      br = new BufferedReader(new FileReader("filepath")); 
      if (linecount % 2 != 0) { 
       linecount ++; 
      } 
      Teams = new String[linecount]; 
      String teamcounter; 
      int arraycount = 0; 
      while ((teamcounter = br.readLine()) != null) { 
       Teams[arraycount] = teamcounter; 
       arraycount++; 
       } 
      return Teams; 
      } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 

      return null;   
    } 

     public static void main(String args[]){ 
      FileRead fr = new FileRead(); 
      for(int i =0; i <fr.ImportTeams().length; i++){ 
       System.out.println(fr.ImportTeams(i)); 
      } 




     } 
} 

回答

4
System.out.println(fr.ImportTeams(i)); 

應該是:

System.out.println(fr.ImportTeams()[i]); 

當您訪問array中的元素時,您需要使用array[index]語法。

+0

啊,我知道這將是小的東西好了。非常感謝!它不會讓我接受另外12分鐘的答案,但我一定會這樣做 – Killian

+0

@基利安:不客氣。祝你好運! – kosa

1
System.out.println(fr.ImportTeams(i)); 

您的方法ImportTeams沒有任何參數。

使用

System.out.println(fr.ImportTeams()[i]);