0

I am new to Java.

I am using some code from a web tutorial.

Its not complete. at the moment I am getting the error 'Cannot find symbol for some variables, but these variables have been declared previously.

For example bf and file_to_read have been declared.

Please can anyone advise?

package cyproperty;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;


public class ReadFile 
   {
    private String path;

    public ReadFile(String file_path)
    {
        path= file_path;
   }

    public String[] OpenFile() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;
        String[] textData = new String[numberOfLines];

        int i;

        for (i=0; i <numberOfLines; i++)
            {

                textData[i] = textReader.readLine();
            }
        textReader.close();

        return textData;
      }

    int readLines() throws IOException 

        (

        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = bf.readline()) !=null)
        {
            numberOfLines++;
        }

        bf.close();

        return numberOfLines;
Vishnu
  • 176
  • 1
  • 2
  • 19
Mesogi
  • 629
  • 1
  • 5
  • 11
  • It doesn't help that you've got `(` instead of `{` at the start of your `readLines` method... I'd also suggest that you get your IDE to format your code - it'll make it clearer, and also highlight problems... – Jon Skeet Jul 02 '15 at 09:09
  • The code, as it is, contains typos and does not even have matching brackets. Try downloading a good IDE (e.g. Eclipse, Netbeans, or IntelliJ) and put your code there, they will highlight code compilation errors for you. – Ibrahim Arief Jul 02 '15 at 09:11
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Raedwald Feb 26 '16 at 20:20

3 Answers3

1

You had a few errors:

Your class wasn't completed properly (inserted } in the end).
bf.readline() doesn't exist its bf.readLine() (capital L).
Your method readlines started with ( instead of {.

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

public class ReadFile  {
    private String path;

    public ReadFile (String file_path) {
        path = file_path;
    }

    public String[] OpenFile() throws IOException {
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;
        String[] textData = new String[numberOfLines];

        int i;

        for (i = 0; i < numberOfLines; i++) {

            textData[i] = textReader.readLine();
        }
        textReader.close();

        return textData;
    }

    int readLines() throws IOException {
        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = bf.readLine()) != null) {
            numberOfLines++;
        }

        bf.close();

        return numberOfLines;
    }
}
Tom Jonckheere
  • 1,630
  • 3
  • 20
  • 37
0

You could try this, :)

package cyproperty;

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

public class ReadFile {

    private String path;

    public ReadFile(String file_path) {
        this.path = file_path;
    }

    public String[] OpenFile() throws IOException {
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = 3;
        String[] textData = new String[numberOfLines];

        for (int i = 0; i < numberOfLines; i++) {
            textData[i] = textReader.readLine();
        }
        textReader.close();

        return textData;
    }

    int readLines() throws IOException {

        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = bf.readline()) != null) {
            numberOfLines++;
        }

        bf.close();

        return numberOfLines;
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
Toby Drane
  • 33
  • 1
  • 7
0

You have a couple of errors :

int readLines() throws IOException 
{ // was (
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while ((aLine = bf.readLine()) !=null){ // was readline
        numberOfLines++;
    }

    bf.close();

    return numberOfLines;
} // was missing
Eran
  • 387,369
  • 54
  • 702
  • 768