Java 从文件中读取双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1855753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading double values from a file
提问by Mateusz Dymczyk
I'm trying to read some numbers (double) from a file and store them in an ArrayList and an array (yes, I need both) with the code below:
我正在尝试从文件中读取一些数字(双精度)并将它们存储在一个 ArrayList 和一个数组中(是的,我需要两个),代码如下:
try {
Scanner scan = new Scanner(file).useDelimiter("\s*\n");
while(scan.hasNextDouble())
{
tmp.add(scan.nextDouble());
}
Double[][] tmp2 = new Double[tmp.size()/2][2];
int tmp3 = 0;
for(int i = 0; i < tmp.size()/2; i++)
{
for(int j = 0; j < 2; j++)
{
tmp2[i][j] = tmp.get(tmp3);
tmp3++;
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
The file I'm trying to read is:
我试图阅读的文件是:
0.0 0.0
0.023 0.023
0.05 0.05
0.2 0.2
0.5 0.5
0.8 0.8
0.950 0.950
0.977 0.977
1.0 1.0
But well my code doesn't work, the hasNextDouble() function doesn't find anything, what am I doing wrong?
但是我的代码不起作用,hasNextDouble() 函数没有找到任何东西,我做错了什么?
EDIT: ok so I edited the source a bit (changed from Object[][] to Double[][]) and added inserting values into the array after they were inserted into the ArrayList, but it still doesn't work - the 'while' loop isn't executed a single time.
编辑:好的,所以我对源代码进行了一些编辑(从 Object[][] 更改为 Double[][])并在将它们插入到 ArrayList 后将插入值添加到数组中,但它仍然不起作用 - ' while' 循环不会执行一次。
采纳答案by Bill the Lizard
I tried reducing the code down to only test the Scanner by itself. The following code works with your data file:
我尝试减少代码以仅测试扫描仪本身。以下代码适用于您的数据文件:
public static void main(String[] args) {
Scanner scan;
File file = new File("resources\scannertester\data.txt");
try {
scan = new Scanner(file);
while(scan.hasNextDouble())
{
System.out.println( scan.nextDouble() );
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
I got the following (expected) output:
我得到以下(预期)输出:
0.0
0.0
0.023
0.023
0.05
0.05
0.2
0.2
0.5
0.5
0.8
0.8
0.95
0.95
0.977
0.977
1.0
1.0
Try this to make sure you're referencing the correct file.
试试这个以确保您引用了正确的文件。
回答by Carl Smotricz
Below is my rendition of your code, adapted to make it run. It immediately explodes with an array indexing exceptions.
以下是我对您的代码的演绎,经过调整以使其运行。它立即因数组索引异常而爆炸。
So: Can you give us a little more framework? What's different from what I did?
所以:你能给我们多一点框架吗?和我做的有什么不同?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Zenzen {
private static ArrayList<Double> tmp = new ArrayList<Double>();
private static File file = new File("Zenzen.dat");
public static void main(String[] args) {
Scanner scan;
try {
scan = new Scanner(file);
Object[][] tmp2 = new Object[tmp.size() / 2][2];
int tmp3 = 0;
while (scan.hasNextDouble()) {
tmp.add(scan.nextDouble());
System.out.println(Arrays.deepToString(tmp.toArray())); // debug print
for (int i = 0; i < tmp.size() / 2; i++) {
for (int j = 0; j < 2; j++) {
tmp2[i][j] = tmp.get(tmp3);
tmp3++;
}
}
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
}
[0.0]
[0.0, 0.0]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Zenzen.main(Zenzen.java:26)
回答by MindIbniM
I had the same problem (not working scanner) and the solution seems to be surprisingly easy. You just need to set a locale for it.
我遇到了同样的问题(扫描仪不工作),解决方案似乎非常简单。你只需要为它设置一个语言环境。
// use US locale to be able to identify doubles in the string
scanner.useLocale(Locale.US);
taken from here: http://www.tutorialspoint.com/java/util/scanner_nextdouble.htm
取自这里:http: //www.tutorialspoint.com/java/util/scanner_nextdouble.htm