Panthy
Panthy

Reputation: 1625

Calling Methods in Main Class and Other Class Differences in Java

I have 3 classes, Mainn, ReadFile, and Entry.

ReadFile is basically my class that does all file i/o stuff.

How come I am able to access ReadFile in my Mainn class just fine, but when I try to access it in Entry "e.openFile()" i get an error that says identifier expected.

I know this can be fixed by making an overloaded method openFile() in Entry but why is this needed in Entry, but not in the main class Mainn?

package homework6;

public class mainn {
    public static void main(String[] args){
        ReadFile r = new ReadFile();
        r.openFile();
        //r.readFile();
        r.skipFirst();
        String x[] = r.getData();
        String y[] = r.getData();
        String z[] = r.getData();

        System.out.println(x[0] + "," + x[1]);
        System.out.println(y[0] + "," + y[1]);
        System.out.println(z[0] + "," + z[1]);

        r.closeFile();
    }
}

ReadFile:

package homework6;
import java.util.*;
import java.io.*;

public class ReadFile {
    Scanner x = null;

    public void openFile(){
        try{
            x = new Scanner(new FileInputStream(
                    "C:\\Users\\Rohan Vidyarthi\\workspace\\Data.csv"));
        }
        catch(FileNotFoundException e){
            System.out.println("File not found error");
        }
    }

    public void readFile(){
        while (x.hasNextLine())
            System.out.println(x.nextLine());
    }

    public void skipFirst(){
        x.nextLine();
    }

    public String[] getData(){ //returns String[] with Date and ADJ Close
        String[] temp;
        String[] out = new String[2]; 
        temp = (x.nextLine()).split(",");
        out[0] = temp[0];
        out[1] = temp[6];
        return out;
    }

    public boolean checker(){
        return x.hasNextLine();
    }

    public void closeFile(){
        x.close();
    }
}

class Entry:

package homework6;

public class Entry extends ReadFile{
    ReadFile e = new ReadFile();
    e.openFile();

    double minn = Double.MAX_VALUE;
    double maxx = Double.MIN_VALUE;

    /*public String[] rMax(){
        String[] temp1;
        String[] temp2;
    }
    */
}

Upvotes: 1

Views: 203

Answers (3)

Vasu
Vasu

Reputation: 22422

I suggest you move your openFile() logic to the ReadFile class constructor as shown below and this approach will give you two advantages:

(1)scanner (which is a mandatory variable of ReadFile class) gets initialized inside the class constructor which makes more sense and avoids all NullPointerException i.e., someone accidentally calling other methods first before openFile() (Always ensure that all mandatory instance variables i.e., data is being initialized by the constructors, I strongly suggest make it as a practice and never allow any object being created freely without being the mandatory variables initialized through constructors which will avoid most of the issues).

(2) It will fix your problem automatically as you don't need a call to openFile() method (well, you don't have that method itself, ReadFile constructor has initialized the scanner).

public class ReadFile {
    Scanner x = null;

    public ReadFile() {
        try{
            x = new Scanner(new FileInputStream(
                    "C:\\Users\\Rohan Vidyarthi\\workspace\\Data.csv"));
        }
        catch(FileNotFoundException e){
            System.out.println("File not found error");
        }
    }

    public void readFile(){
        //add code
    }

    public void skipFirst(){
        //add code
    }

    public String[] getData(){
        //add code
    }

    public boolean checker(){
        return x.hasNextLine();
    }

    public void closeFile(){
        x.close();
    }
  }

Just ensure that you don't need to call openFile() anymore as shown below:

public class Entry extends ReadFile{
    ReadFile e = new ReadFile();//initializes scanner as well

    public String[] readFile() {//add any methods you like here in this like
        return e.readFile();
    }

    double minn = Double.MAX_VALUE;
    double maxx = Double.MIN_VALUE;
}

How come I am able to access ReadFile in my Mainn class just fine, but when I try to access it in Entry "e.openFile()" I get an error that says identifier expected.

In Java, invocation of any method call (like your r.openFile()) should be done from another method or from constructor or from initializers (static or instance initializer), so the answer is in your Mainn class, you are calling the openFile() inside from main(String[] args) method whereas in your Entry class your openFile() method call is not wrapped inside any of the above-mentioned code blocks (i.e., methods, constructors, initializers).

One more important point is that in general when you say A extends B in Object Oriented Languages, it means that A IS-A type of B, but in your code Entry extends ReadFile does not make much sense, so you should avoid that.

Upvotes: 1

stinepike
stinepike

Reputation: 54672

put e.openFile(); in a method or constructor. You cannot place floating codes outside methods. Any statement can only be used inside the block of codes (i.e. methods, constructors, static initializers)

Upvotes: 1

Pavlo Plynko
Pavlo Plynko

Reputation: 586

If you do

public class mainn {
    ReadFile r = new ReadFile();
    r.openFile();
    //r.readFile();
    r.skipFirst();
    String x[] = r.getData();
        ...

you will receive the same error in mainn

Upvotes: 0

Related Questions