2014-11-22 105 views
0
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类,因为这是我第一次不得不这样做。 任何想法?另外,如何格式化文本文件的输出以便将其显示在表格中?有没有办法呢?错误更改系统找不到指定的文件

+0

尝试'new File(“survey.txt”)',即添加扩展名。 – A4L 2014-11-22 19:20:30

+0

尝试了新的文件(“survey.txt”)以及许多其他格式(如src/p1/survey.txt),并且还尝试将survey.txt放入我的项目的每个文件夹中,并仅使用名称。似乎没有什么能让程序运行起来。 – TheUnknown 2014-11-22 19:21:52

+0

检查文件是否存在于您期望的位置,将此语句添加到您的主要方法中,以查看您的工作目录是什么'System.out.println(new File(“”)。getAbsolutePath());'。使用'new File(“survey.txt”)'表示文件必须是语句将打印的目录。 – A4L 2014-11-22 19:25:20

回答

1

错误“无法找到主类”与您使用文件或扫描程序类的方式无关。这听起来像你的类路径是错误的。确保您的项目根目录已正确配置。您可以尝试使用this tutorial for using Eclipse以了解如何正确设置项目。如果您没有使用IDE,请检查您正在运行的文件的内容,并确保其中包含正确的信息。如果你可以更多地指定你的问题,比如你正在使用哪个操作系统,你是否使用IDE(如果是,哪一个),你是将它编译为jar文件,还是你正在运行它与类文件的目录...等

+0

这些项目由教授设置。我们的工作是解决我们提出的问题,并回到他身边,我只是测试了这个程序,它似乎没有运行我的任何代码。将尝试重新下载该文件。 – TheUnknown 2014-11-22 19:56:22

+0

你能告诉我你下载的文件的内容是什么样的吗?你是用特定的程序打开它,还是只用文本编辑器编辑文件?你如何运行它,你是否点击一个文件来运行它(例如.bat文件或linux上的bash/shell脚本)? – 2014-11-22 19:59:11

+0

我们下载一个包含checkstyle,build和src文件的zip文件。一切都设置好了,当我们运行ant时,我们得到了我们提交给prof的文件。 Src包含4个包,每个包含1到2个类。这里是一个imgur http://imgur.com/PVnidkm – TheUnknown 2014-11-22 20:03:14

0

更改阵列翻一番,而不是整数,改变了扫描线,以这样的: 扫描仪SC =新的扫描仪( 新的文件(“src”用户+文件分割符+“调查。文本”));

相关问题