Question

I am consider a rookie and I have searched for hours on the internet to solve my problem but still no luck.

I really want to understand java and if you could explain some detail that will be highly grateful.

The problem is in this line

ReadFile file = ReadFile(file_name);

error message : "ReadFile cannot be resolved to  a type."

Here is my code: FileData.java

package textfiles;
import java.io.IOException;

public class FileData {

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

        String file_name = "D:/java/readfile/test.txt";

        try {

            ReadFile file = ReadFile(file_name);
            String[] aryLines = file.OpenFile();


            int i;
            for (i =0; i < aryLines.length ; i++) {

                System.out.println( aryLines[i] );
            }

        }
        catch (IOException e) {

            System.out.println( e.getMessage() );
        }

    }       
}

And this is my other code: ReadFile.java

package textfiles;

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;

    }
    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;
    }
    public  String[] OpenFile () throws IOException {

        FileReader fr = new FileReader (path);
        BufferedReader textReader = new BufferedReader (fr);


        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

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

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


        textReader.close();
        return textData;
    }
}
Was it helpful?

Solution

Try this:

ReadFile file = new ReadFile(file_name);

In order to initialize an object with it's class name you should use the new key word like this:

ClassName objName = new ClassName(arguments);

From the rest of your code seems you know the notion, nevertheless I refer you (or possible future visitors) to this page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top