1,105,263 Community Members

making an array

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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?

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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?

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

isnt this it?

int readLines() throws IOException {

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

it still doesnt seem to want to work...

What happens?

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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)

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Glad you got it working.

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how do i mark this as solved?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: