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类,因为这是我第一次不得不这样做。 任何想法?另外,如何格式化文本文件的输出以便将其显示在表格中?有没有办法呢?错误更改系统找不到指定的文件
尝试'new File(“survey.txt”)',即添加扩展名。 – A4L 2014-11-22 19:20:30
尝试了新的文件(“survey.txt”)以及许多其他格式(如src/p1/survey.txt),并且还尝试将survey.txt放入我的项目的每个文件夹中,并仅使用名称。似乎没有什么能让程序运行起来。 – TheUnknown 2014-11-22 19:21:52
检查文件是否存在于您期望的位置,将此语句添加到您的主要方法中,以查看您的工作目录是什么'System.out.println(new File(“”)。getAbsolutePath());'。使用'new File(“survey.txt”)'表示文件必须是语句将打印的目录。 – A4L 2014-11-22 19:25:20