My program keeps saying that the method cannot be resolved

这一生的挚爱 提交于 2019-12-31 05:13:18

问题


This program should tell me if it can find the file I am naming. Eclipse has no red lines but every time I run it I get this error message and I don't know why. Thank you in advance for any help you can provide.

import java.io.File;

public class StockMarket {
    public StockMarket(String[] args) {
        ReadFiles r = new ReadFiles();
        System.out.println(r.checkIsFile());
    }
}

and the second class

import java.io.*; 
import java.util.StringTokenizer; 

public class ReadFiles {
    public static void main(String[] args) {
        System.out.println("Test");
        File file = new File("C:\\stocks\\yahoo.csv");
        int row = 0;
        String[] [] items;
    }

    public boolean checkIsFile() {
        File file= new File("C:\\stocks\\yahoo.csv");
        return file.isFile();
    }
}

Error Message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
checkIsFile cannot be resolved or is not a field
    at StockMarket.main(StockMarket.java:6)

回答1:


A few things to try.

It looks like your ReadFiles class is not being compiled.

You might try adding an import statement for your ReadFiles class to your main.

Do a clean build.

Also be aware that occasionally Eclipse goes "weird" and you need to do some combination of close it reopen it and/or clean the workspace and/or reboot. I know you would think with modern software... but still it happens.

Here are a couple of links that may help.

Keeping Eclipse running clean

Restarting Eclipse Clean If You Cannot Run Eclipse From A Command Line (Mac OSX)

As an aside, on some prior projects and versions of eclipse I found myself having to do this so frequently that I ended up just setting up eclipse to always launch with the clean option.




回答2:


System.out.println(r.checkIsFile());

should be in the static main function, you are going the wrong way for the flow of code...



来源:https://stackoverflow.com/questions/18199350/my-program-keeps-saying-that-the-method-cannot-be-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!