0

我试图打印出数组的内容,并且在循环遍历数组以打印元素时遇到了问题。它给我的错误是:

FileRead 类型中的 ImportTeams() 方法不适用于参数 (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

2 回答 2

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

应该 :

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

当您从 访问元素时array,您需要使用array[index]语法。

于 2013-07-09T15:08:44.450 回答
1
System.out.println(fr.ImportTeams(i));

您的方法 ImportTeams 没有任何参数。

利用

System.out.println(fr.ImportTeams()[i]);
于 2013-07-09T15:09:07.793 回答