Hi, I am trying to make an array to read txt files. I have hard coded it to 3 for the array but I would prefer for it to be able to read the txt files to see how big the array should be. I have implemented some code to this but I am having trouble getting it to work.

package searchengine2;

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

/**
 *
 * @author Alex
 */
public class ReadFile {
     private String path;
    
            int readLines() throws IOException {
            
            FileReader file_to_read = new FileReader(path);
            BufferedReader textReader = new BufferedReader(file_to_read);
            
            String aLine;
            int numberOfLines = 0;
            
            while (( aLine = bf.readLine()) !=null) {
                numberOfLines++;
            }
            bf.close();
            
            return numberOfLines;
            }
            
            
    public ReadFile(String file_path) {
        path = file_path;
            
    }
    
    public String[] OpenFile() throws IOException {
        FileReader fr = new FileReader(path);               //reads characters
        BufferedReader textReader = new BufferedReader(fr); //used to store characters
        
        int numberOfLines = readLines();                              //number of lines in array
        String[] textData = new String[numberOfLines];      //set up array
        
        int i;
                                                               
        for (i=0; i < numberOfLines; i++) {                 //loop to put data into array
            textData[i] = textReader.readLine();            //access the data, textReader holding data in memory (buffer)
        }
        textReader.close();                                 //removes data from buffer
        return textData;
        }

}

Many Thanks if you can help!

Recommended Answers

All 17 Replies

I am having trouble getting it to work.

Please explain the problem you are having.

Another (and better) approach would be to use an ArrayList to hold the lines from the file.

when trying to run it, i have an error. I think i have made an error in the coding but not too sure where? i think I might have placed the coding to work out the number needed for the array in the wrong place but also no idea where else i should put it...

when trying to run it, i have an error

If you want help with the error, you must post the full text of the message here.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: bf.readLine
at searchengine2.ReadFile.readLines(ReadFile.java:23)
at searchengine2.ReadFile.OpenFile(ReadFile.java:41)
at searchengine2.SearchEngine2.main(SearchEngine2.java:22)
Java Result: 1

Can you compile your code and generate error messages showing error message and the source line and the line number where the error occurred?
Here is what I get when I compile with an error:
Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

Where is the variable bf defined?

this is all the error messages I got :(

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable readLines
location: class searchengine2.ReadFile
at searchengine2.ReadFile.OpenFile(ReadFile.java:43)
at searchengine2.SearchEngine2.main(SearchEngine2.java:22)
Java Result: 1

I tried following this guide:
http://www.homeandlearn.co.uk/java/read_a_textfile_in_java.html

Where is the variable: readLines defined?

There is a method: readLines()
but the error message says variable, not method. Did you leave off the ()s?

isnt this it?

int readLines() throws IOException {

The error message says variable, not method???
What do you find if you Search your code for readLines?

sorry im a complete newbie at this... i dont really understand what you mean?

Use the Edit Search menu item in your editor/IDe to find all the occurrences of readLines in your program.
Do they all end with ()s or are they any without the ending ()?

The error message you posted:
symbol: variable readLines

says that the missing symbol is a variable, a method would end with ()s

one of the readLines didnt have the () but I have now placed one there but it still doesnt seem to want to work...

it still doesnt seem to want to work...

What happens?

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: bf.readLine
at searchengine2.ReadFile.readLines(ReadFile.java:25)
at searchengine2.ReadFile.OpenFile(ReadFile.java:43)
at searchengine2.SearchEngine2.main(SearchEngine2.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

i fixed the issue... after you mentioned not declaring bf i had a look back and noticed i typed in fileReader where I was meant to type bf. my bad... thanks for your help!

Glad you got it working.

how do i mark this as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.