java将文件的输出存储到数组中 [英] java storing output from file into an array

查看:188
本文介绍了java将文件的输出存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何将文件的输出存储到数组有疑问。就我而言,我试图将文件中的所有日期存储到数组中。对于我这样做的方式,编译器抱怨不是语句。我怎样才能解决这个问题?任何帮助将不胜感激。下面是我的代码(错误行之前是反斜杠):

I have a question on how to store output from a file into an array. In my case, I am trying to store all the date in a file into an array. For the way that I did it, the compiler complains "not a statement". How can I fix this? Any help will be greatly appreciated. Below is my code (the line of error is preceded by backslashes):

double token[] = new double[9];

            File filename = new File("/Users/timothylee/gravity1.txt");

            Scanner inFile = new Scanner(filename);

            while(inFile.hasNext()){
               //////////////// token[] = inFile.nextDouble();
                System.out.println(token);
            }
            inFile.close();

如果需要,这里是文件:

If needed, here is the file:

gravity1.txt:
      3.70
      8.87
      9.79
      3.70
      24.78
      10.44
      8.86
      11.13
      0.61


推荐答案

double token[] = new double[9];

int i = 0;
while(inFile.hasNext()){
    token[i] = inFile.nextDouble();
    System.out.println(token);
    i++;
}
inFile.close();

这是假设文件只有9行。

This is assuming the file only has 9 lines.

这篇关于java将文件的输出存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆