Java - Error: Can not find symbol after try ... catch (fileContent [0])

0
public class ShowDoMilhao {

//constructor of the class
public ShowDoMilhao()
{

}

public static void main(String[] args) throws IOException
{
    //string to hold name of txt file
    String file1 = "Facil.txt";

    ////////////////////////////////////////////////////////////
    //Storing file into an array of strings
    try {
       //creating an object (an instance of ReadFile class)
       ReadFile file = new ReadFile(file1);

       String[] fileContent = new String[file.readLines()];
       fileContent = file.OpenFile();  

    }
    catch (IOException e) {
        System.out.println(e.getMessage());
    }
    ////////////////////////////////////////////////////////////   

    System.out.println(fileContent[0]);                
}   //end of main method   
}//end of class
    
asked by anonymous 18.06.2017 / 19:46

1 answer

0

The error occurs because you are trying to access fileContent out of try catch .

Declare the variable before it is accessible:

public class ShowDoMilhao {

    public ShowDoMilhao() {

    }

    public static void main(String[] args) throws IOException {

        String file1 = "Facil.txt";

        String[] fileContent;
        try {
            ReadFile file = new ReadFile(file1);

            fileContent = new String[file.readLines()];
            fileContent = file.OpenFile();

        }catch (IOException e) {
            System.out.println(e.getMessage());
        }

        System.out.println(fileContent[0]);
    }
}

Note that I made the declaration of the variable out of try catch , otherwise it is visible only in the scope of try catch

    
18.06.2017 / 20:55