将 .txt 文件中的双精度数读取到列表中
我正在尝试从 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataInputStream
实际上用于读取二进制数据,例如从套接字读取二进制数据。我认为你想要的只是简单的FileReader
,然后使用Double.parseDouble(String)
或Double.valueOf(String)
进行解析 - 取决于无论你想获得原始类型还是对象类型。如果您的输入文件在新行中包含每个数字,则可以使用 BufferedReader.readLine。否则,您将需要一些简单的解析,例如查找空格或逗号分隔的值。
例如,要读取一个新行中包含每个数字的文件,您可以执行以下操作:
但是,如果您的文件以不同的方式分隔数字(例如空格或逗号),您将需要在读取时做一些额外/不同的工作文件来提取(解析)您的值。
解析是关于对数据进行标记,以便您可以提取您感兴趣的位。
DataInputStream
is really for reading binary data, e.g. from a socket. I think what you want is just simpleFileReader
and then parse usingDouble.parseDouble(String)
orDouble.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:
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.
这可能是“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".
DataInputStream
仅用于读取使用相应输出流写入的内容。对于文本文件,请在FileInputStream
周围创建一个InputStreamReader
(记住指定编码),然后创建一个BufferedReader
,以便您可以通过线。然后使用 Double 类来解析字符串。DataInputStream
is only for reading things written with the corresponding output stream. For a text file, create anInputStreamReader
around yourFileInputStream
(remembering to specify the encoding), and then aBufferedReader
so that you can read by lines. Then use the Double class to parse the strings.