简体   繁体   English

为什么我的扫描仪无法读取文件?

[英]Why is my scanner not reading from my file?

I'm trying to get my program to read data from a text file and store it in an array. 我正在尝试让我的程序从文本文件读取数据并将其存储在数组中。 The text file contains data about a planet. 文本文件包含有关行星的数据。 Here is an example: 这是一个例子:

Mercury
4.151002e10
2.642029e10
-1.714167e9
-3.518882e4
4.355473e4
6.785804e3
3.302e23

My file is named test.txt. 我的文件名为test.txt。 It lives in the same directory as my class.java file. 它与我的class.java文件位于同一目录中。 I've used System.out.println(new File("test.txt").getAbsolutePath()); 我用过System.out.println(new File("test.txt").getAbsolutePath()); to check if the directory path is correct, which it was, and I used System.out.println(new File(".")); 检查目录路径是否正确,这是正确的,并且我使用了System.out.println(new File(".")); to check if it was in the same directory that the code was trying to compile in, which again it was (outputted just a dot which I was led to believe meant it was in the correct directory). 检查它是否位于代码试图在同一目录中,然后再次被编译(仅输出一个点,我认为这意味着它位于正确的目录中)。 I've tried different ways of finding the file, such as renaming it to something else to check it wasn't a keyword, changing the encoding of the file to Unicode, or UTF-8, or ANSI, none of which worked, using .\\test in the file to look in the same directory, none of which worked. 我尝试了多种查找文件的方式,例如将其重命名为其他内容以检查它是否不是关键字,将文件的编码更改为Unicode或UTF-8或ANSI,但都无效, .\\test在文件中查找相同的目录,没有一个起作用。

Here is my code: 这是我的代码:

public static void defaultPlanetArray(){
  Planet[] solarSystem;
  solarSystem = new Planet[9];
  PhysicsVector dummyAcceleration = new PhysicsVector();

  System.out.println(new File("test.txt").getAbsolutePath());
  System.out.println(new File("."));

  try{
    File file = new File("C:\\Users\\Lizi\\Documents\\Uni Work\\Year 2\\PHYS281\\Project\\test.txt");
    Scanner scnr = new Scanner(file);
  }
  catch(FileNotFoundException e){
    System.out.println("File not found!");
  }
  int i = 0;

  while(i<9 && scnr.hasNextLine()){
    //read values from file and set as Planet object, then set to array.
    i++
  }

PhysicsVector and Planet are both classes I have created. PhysicsVector和Planet都是我创建的类。 PhysicsVector and the rest of Planet apart from this excerpt compile with no problems. 除此摘录外,PhysicsVector和Planet的其余部分都可以毫无问题地进行编译。 When I try to compile this specific bit of code, I get: 当我尝试编译这段特定的代码时,我得到:

.\\Planet.java:65: error: cannot find symbol while(i<9 && scnr.hasNextLine()){ ^

I'm guessing this means that the variable scnr is not being created in the try section because it cannot find the file. 我猜这意味着在try部分中未创建变量scnr,因为它找不到文件。 I think this because when I don't include the try and catch blocks, I get: 我认为这是因为当我不包括try和catch块时,我得到:

.\\Planet.java:59: error: unreported exception FileNotFoundException; must be caught or declared to be thrown Scanner scnr = new Scanner(file); ^

I've also tried the catches FileNotFoundException when I'm first creating the method but that gives me the same error as immediately above. 当我第一次创建方法时,我也尝试过catches FileNotFoundException ,但这给了我与上面相同的错误。

I could just set the values in the program, but that would give a lot of unnecessary code and be rather inefficient I think. 我可以只在程序中设置值,但是那会产生很多不必要的代码,而且我认为效率很低。

So my question is, how do I get the scanner to read my values from the file? 所以我的问题是,如何让扫描仪从文件中读取值?

As @Lalit Verma pointed the scnr variable you defined lives inside the try - catch block. 正如@Lalit Verma指出的scnr您定义的scnr变量位于try-catch块中。

Change the code to: 将代码更改为:

try{
    File file = new File("C:\\Users\\Lizi\\Documents\\Uni Work\\Year 2\\PHYS281\\Project\\test.txt");
    Scanner scnr = new Scanner(file);
    int i = 0;

    while(i<9 && scnr.hasNextLine()){
      //read values from file and set as Planet object, then set to array.
      i++
    }
  }catch(FileNotFoundException e){
    System.out.println("File not found!");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM