错误更改系统无法找到指定的文件

时间:2014-11-22 19:18:41

标签: java file runtime-error

package q1;

import java.io.FileNotFoundException;
import java.util.ArrayList;


/**
 * <p>This is where you put your description about what this class does. You
 * don't have to write an essay but you should describe exactly what it does.
 * Describing it will help you to understand the programming problem better.</p>
 *
 * @author Your Name goes here
* @version 1.0
*/
public class Household {
 /**
 * <p>This is the main method (entry point) that gets called by the JVM.</p>
 *
 * @param args command line arguments.
 * @throws FileNotFoundException 
 */
 public static void main(String[] args) throws FileNotFoundException {
    // your code will go here!!!
    Survey s1 = new Survey();
    s1.getSurveyList();
    System.out.println();
}

};
-------------------------------------------------------------------------------------------

        package q1;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;

    public class Survey {
        ArrayList<Integer> surveyList = new ArrayList<Integer>();

        public Survey(){

        }

        public ArrayList<Integer> getSurveyList() throws FileNotFoundException{
            Scanner sc = new Scanner(new File("survey.txt"));

            while (sc.hasNextLine()) {
                sc.useDelimiter(" ");
                int i = sc.nextInt();
                surveyList.add(i);
            }

            System.out.println(surveyList.get(0));
            sc.close();

            return surveyList;
        }


    }

现在它说系统找不到指定的文件。不知道如何使用File类,因为这是我第一次不得不这样做。 有任何想法吗?另外,如何格式化文本文件的输出以便将其显示在表格中?是否有某种方法可以做到这一点?

2 个答案:

答案 0 :(得分:1)

错误&#34;找不到主要课程&#34;与您使用File或Scanner类的方式无关。听起来你的类路径是错误的。确保正确配置了项目根目录。您可以尝试使用this tutorial for using Eclipse作为有关如何正确设置项目的参考。如果您不使用IDE,请检查您正在运行的文件的内容,并确保其中包含正确的信息。如果您可以更多地指定您的问题,例如您使用的操作系统,使用IDE(如果是,哪一个),您是将其编译为jar文件还是从中运行它,那将会有很大帮助包含类文件的目录......等等。

答案 1 :(得分:0)

将Array更改为Double而不是Integer,并将扫描仪行更改为:     扫描仪sc =新扫描仪(                 新文件(“src”+ File.separator +“survey.txt”));

相关问题