将 .txt 文件中的双精度数读取到列表中

发布于 2024-10-28 14:24:26 字数 1561 浏览 6 评论 0原文

我正在尝试从 .txt 文件中读取双精度值并将它们全部放入列表中。

到目前为止,这是我的代码 ->

(我有一种方法来请求文件并获取数据流,还有一种方法将双精度数放入列表中。)

    public InputFile () throws MyException {
    fileIn = null;
    dataIn = null;
    do {
        filename = JOptionPane.showInputDialog("What is the file called? ");
        try {
            fileIn = new FileInputStream((filename + ".txt"));
            dataIn = new DataInputStream(fileIn);

            return;
        }
            catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "There is no "+filename + ".txt");
        }
   }
   while (   Question.answerIsYesTo("Do you want to retype the file name")   );
   throw new MyException("No input file was chosen");
}

该部分工作正常。

        public ProcessMain() {
        boolean EOF = false;

    List <Double> allNumbers = new ArrayList <Double> () ;

            try {

                InputFile inputFile = new InputFile();

                        while(EOF == false) {
                        try {
                          allNumbers.add(inputFile.dataIn.readDouble());
                            }
                        catch(EOFException e){ EOF = true; }
                        }

         // List manipulation here. I have no problems with this part.

    }
    catch (Exception e) {
        System.out.println(e);
    }

     System.out.println(allNumbers);

我得到以下信息 -

java.lang.IndexOutOfBoundsException:索引:0,大小:0

有什么想法吗?

I'm trying to read doubles from a .txt file and put them all into a list.

Here's my code so far ->

(I have one method to ask for the file and get the data stream, and one to put the doubles into a list.)

    public InputFile () throws MyException {
    fileIn = null;
    dataIn = null;
    do {
        filename = JOptionPane.showInputDialog("What is the file called? ");
        try {
            fileIn = new FileInputStream((filename + ".txt"));
            dataIn = new DataInputStream(fileIn);

            return;
        }
            catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "There is no "+filename + ".txt");
        }
   }
   while (   Question.answerIsYesTo("Do you want to retype the file name")   );
   throw new MyException("No input file was chosen");
}

That part works fine.

        public ProcessMain() {
        boolean EOF = false;

    List <Double> allNumbers = new ArrayList <Double> () ;

            try {

                InputFile inputFile = new InputFile();

                        while(EOF == false) {
                        try {
                          allNumbers.add(inputFile.dataIn.readDouble());
                            }
                        catch(EOFException e){ EOF = true; }
                        }

         // List manipulation here. I have no problems with this part.

    }
    catch (Exception e) {
        System.out.println(e);
    }

     System.out.println(allNumbers);

I get the following -

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深如许 2024-11-04 14:24:26

DataInputStream 实际上用于读取二进制数据,例如从套接字读取二进制数据。我认为你想要的只是简单的 FileReader ,然后使用 Double.parseDouble(String)Double.valueOf(String) 进行解析 - 取决于无论你想获得原始类型还是对象类型。

如果您的输入文件在新行中包含每个数字,则可以使用 BufferedReader.readLine。否则,您将需要一些简单的解析,例如查找空格或逗号分隔的值。

例如,要读取一个新行中包含每个数字的文件,您可以执行以下操作:

import java.io.*;

public class DoubleReader {
  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(args[0]));
    String line = null;
    while((line = reader.readLine()) != null) {
        Double d = Double.valueOf(line);
        System.err.println(d);
    }
  }
}

但是,如果您的文件以不同的方式分隔数字(例如空格或逗号),您将需要在读取时做一些额外/不同的工作文件来提取(解析)您的值。

解析是关于对数据进行标记,以便您可以提取您感兴趣的位。

DataInputStream is really for reading binary data, e.g. from a socket. I think what you want is just simple FileReader and then parse using Double.parseDouble(String) or Double.valueOf(String) - depending on whether you want to get primitive or Object doubles.

If your input file contains each number on a new line, you can use BufferedReader.readLine. Otherwise you will need some simple parsing, e.g. to find space or comma-separated values.

So for example, to read a file containing each number on a new line you could do something like this:

import java.io.*;

public class DoubleReader {
  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(args[0]));
    String line = null;
    while((line = reader.readLine()) != null) {
        Double d = Double.valueOf(line);
        System.err.println(d);
    }
  }
}

But if your file separates the numbers differently (e.g. space or comma) you will need to do a bit of extra/different work while reading the file to extract (parse) your values.

Parsing is about tokenizing your data so you can extract the bits you're interested in.

孤独陪着我 2024-11-04 14:24:26

这可能是“EOF = false”行..将“false”分配给“EOF”并且始终为true。尝试将其更改为“EOF == false”。

It's probably the "EOF = false" line.. that assigns "false" to "EOF" and will always be true. Try changing it to "EOF == false".

゛时过境迁 2024-11-04 14:24:26

DataInputStream 仅用于读取使用相应输出流写入的内容。对于文本文件,请在 FileInputStream 周围创建一个 InputStreamReader(记住指定编码),然后创建一个 BufferedReader,以便您可以通过线。然后使用 Double 类来解析字符串。

DataInputStream is only for reading things written with the corresponding output stream. For a text file, create an InputStreamReader around your FileInputStream (remembering to specify the encoding), and then a BufferedReader so that you can read by lines. Then use the Double class to parse the strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文