Killian
Killian

Reputation: 934

Trying to print an array argument error

Im trying to print out the contents of an array and I'm running into an issue when looping through the array to print the elements. The error it is giving me is:

The method ImportTeams() in the type FileRead is not applicable for the arguments (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));
            }




        }
}

Upvotes: 0

Views: 60

Answers (2)

stinepike
stinepike

Reputation: 54672

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

Your method ImportTeams doesn's have any parameter.

use

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

Upvotes: 1

kosa
kosa

Reputation: 66637

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

should be :

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

When you access elements from array, you need to use array[index] syntax.

Upvotes: 4

Related Questions