Java 从文件中读取浮点数
如何从文件中读取浮点数Java 从文件中读取浮点数,java,Java,如何从文件中读取浮点数 0.00000E+00 2.12863E-01 1.00000E-02 2.16248E-01 2.00000E-02 2.19634E-01 在文件中,第一列数字前和数字之间有2个空格。我立即出现错误: s = new Scanner(new File("P0")); while (s.hasNext()) { float x = s.nextFloat(); float y = s.nextFloat(); System.
0.00000E+00 2.12863E-01
1.00000E-02 2.16248E-01
2.00000E-02 2.19634E-01
在文件中,第一列数字前和数字之间有2个空格。我立即出现错误:
s = new Scanner(new File("P0"));
while (s.hasNext()) {
float x = s.nextFloat();
float y = s.nextFloat();
System.out.println("x = " + x + ", y = " + y);
}
BufferedReader reader = null;
try {
// use buffered reader to read line by line
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"<FULL_FILE_PATH>"))));
float x, y;
String line = null;
String[] numbers = null;
// read line by line till end of file
while ((line = reader.readLine()) != null) {
// split each line based on regular expression having
// "any digit followed by one or more spaces".
numbers = line.split("\\d\\s+");
x = Float.valueOf(numbers[0].trim());
y = Float.valueOf(numbers[1].trim());
System.out.println("x:" + x + " y:" + y);
}
} catch (IOException e) {
System.err.println("Exception:" + e.toString());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println("Exception:" + e.toString());
}
}
}
BufferedReader读取器=null;
试一试{
//使用缓冲读取器逐行读取
reader=新的BufferedReader(新的InputStreamReader)(新文件InputStream(新文件(
""))));
浮动x,y;
字符串行=null;
字符串[]数字=null;
//逐行读取,直到文件结束
而((line=reader.readLine())!=null){
//基于正则表达式拆分每一行
//“后跟一个或多个空格的任何数字”。
数字=行。拆分(\\d\\s+);
x=Float.valueOf(数字[0].trim());
y=Float.valueOf(数字[1].trim());
System.out.println(“x:+x+”y:+y);
}
}捕获(IOE异常){
System.err.println(“异常:+e.toString());
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
System.err.println(“异常:+e.toString());
}
}
}
BufferedReader reader = null;
try {
// use buffered reader to read line by line
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"<FULL_FILE_PATH>"))));
float x, y;
String line = null;
String[] numbers = null;
// read line by line till end of file
while ((line = reader.readLine()) != null) {
// split each line based on regular expression having
// "any digit followed by one or more spaces".
numbers = line.split("\\d\\s+");
x = Float.valueOf(numbers[0].trim());
y = Float.valueOf(numbers[1].trim());
System.out.println("x:" + x + " y:" + y);
}
} catch (IOException e) {
System.err.println("Exception:" + e.toString());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println("Exception:" + e.toString());
}
}
}
BufferedReader读取器=null;
试一试{
//使用缓冲读取器逐行读取
reader=新的BufferedReader(新的InputStreamReader)(新文件InputStream(新文件(
""))));
浮动x,y;
字符串行=null;
字符串[]数字=null;
//逐行读取,直到文件结束
而((line=reader.readLine())!=null){
//基于正则表达式拆分每一行
//“后跟一个或多个空格的任何数字”。
数字=行。拆分(\\d\\s+);
x=Float.valueOf(数字[0].trim());
y=Float.valueOf(数字[1].trim());
System.out.println(“x:+x+”y:+y);
}
}捕获(IOE异常){
System.err.println(“异常:+e.toString());
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
System.err.println(“异常:+e.toString());
}
}
}
所以,我理解我的错误。我需要使用
s.useLocale(Locale.US);
因为扫描仪将“.”作为十进制分隔符,在我的语言环境中(默认值)是“,”。还请注意,1.1和3(整数)都可以被nextDouble识别
//根据所以,我理解我的错误。我需要使用
s.useLocale(Locale.US);
因为扫描仪将“.”作为十进制分隔符,在我的语言环境中(默认值)是“,”。还请注意,1.1和3(整数)都可以被nextDouble识别
//根据你现在做得怎么样?@mdl我在下面添加了代码你会遇到什么错误?上帝禁止我建议在(s.hasNextFloat())
时使用。我上次那样做时被火刑柱烧死了。@Denis你的代码对我来说很好。你得到了什么错误?你的数字是什么样子的?你现在做得怎么样?@mdl我在下面添加了代码你得到了什么错误?上帝禁止我建议使用,而(s.hasNextFloat())
。我上次那样做时被火刑柱烧死了。@Denis你的代码对我来说很好。你有什么错误?你的数字是什么样子的?它真的很有效,谢谢!但是每行有2个值,我想把它们应用到变量x和y中。如何更正代码的这一部分?在您的案例中,每行分为三个单词。第一个是空字符串,其他是实际数字。使用索引1和2来获取数字。我应该在哪里使用这些索引?你能帮我处理这一小部分代码吗?我已经根据你的要求更新了代码,并进行了异常处理。它真的很有效,谢谢!但是每行有2个值,我想把它们应用到变量x和y中。如何更正代码的这一部分?在您的案例中,每行分为三个单词。第一个是空字符串,其他是实际数字。使用索引1和2来获取数字。我应该在哪里使用这些索引?你能帮我处理这一小部分代码吗?我已经按照你的要求更新了代码,并进行了异常处理。