I am trying to parse a text file that contains data similar to below:
我试图解析包含类似于下面的数据的文本文件:
%abc
-12 -9 10 150 180
-4.31 -2.29 -0.3689 .0048 4.987 6.123 19
%xyz
Other data, not important to what I am doing%abc -12 -9 10 150 180 -4.31 -2.29 -0.3689 .0048 4.987 6.123 19%xyz其他数据,对我的工作并不重要
I have written this code to parse the top two lines, as well as the other data, except for the placeholder value %abc
:
我编写了这段代码来解析前两行以及其他数据,除了占位符值%abc:
public static void main(String[] args) {
Scanner scan;
File file = new File("kx=2.2_Au.txt");
try {
BufferedReader in = new BufferedReader(new FileReader("kx=2.2_Au.txt"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (IOException e) {
System.out.println("File Read Error");
}
}
From this point, I need to take the max-min for each of the top two lines, and assign their value to a string variable. I am not sure how to go about this part.
从这一点来说,我需要为前两行中的每一行取max-min,并将它们的值赋给字符串变量。我不知道该如何解决这个问题。
Edit:
My code now looks like the following:
我的代码现在如下所示:
public void execute() {
File file = new File("upload:///file1");
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
String[] parts = str.split(" ");
for (String item : parts) {
double min;
double max;
min = (Double.isNaN(min) || min > Double.parseDouble(item) ?
Double.parseDouble(item) : min);
max = (Double.isNaN(max) || max<Double.parseDouble(item) ?
Double.parseDouble(item) : max);
}
//System.out.println(str);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
At this point I'm getting the error that the variables min and max may not have been initialized although they are defined as doubles right before they're used in the code - any more input would be sincerely appreciated. Thanks.
此时我得到的错误是变量min和max可能尚未初始化,尽管它们在代码中被使用之前被定义为双精度 - 任何更多输入都将被真诚地赞赏。谢谢。
1 个解决方案
#1
1
As has been mentioned, you need to initialize your variables, rather than just declaring them. I would highly suggest, from the nature of the comment regarding initializing the variable before the value it will be assigned, that you look at some of the code tutorials that can be found free on the internet. This will give you a better understanding of the basics of the Java language.
如前所述,您需要初始化变量,而不是仅仅声明它们。我强烈建议,从关于在分配值之前初始化变量的注释的性质,你看一些可以在互联网上免费找到的代码教程。这将使您更好地理解Java语言的基础知识。
With that being said, the compiler is directly telling you that there is an issue, and is telling you exactly what it is. Initialize your doubles, and recompile; the errors will indeed go away. If you try to compile prior to making these changes, it is likely you are seeing something similar to this:
话虽如此,编译器直接告诉你存在问题,并且正在告诉你它究竟是什么。初始化你的双打,然后重新编译;错误确实会消失。如果您在进行这些更改之前尝试编译,很可能您会看到与此类似的内容:
Somefile.java:123:error: variable min might not have been initialized
Somefile.java:123:error:变量min可能尚未初始化
min = (Double.isNan(min) || ...
min =(Double.isNan(min)|| ...
Changing this declaration
改变这个声明
double min;
double max;
to instead both declare and initialize the variables
而是声明和初始化变量
double min = 0.00;
double max = 0.00;
will resolve the specific compiler error you mentioned.
将解决您提到的特定编译器错误。
With that being said, there are some other issues that you will face with the implementation that you have. If you know that after a certain point, token, or other symbol within your input file you will not care about any other data, it is suggestible to restrict the lines being read to only those necessary to perform the work.
话虽如此,您将面临一些其他问题,您将面临实施。如果您知道在输入文件中的某个点,令牌或其他符号之后您不关心任何其他数据,则可以将读取的行限制为仅执行工作所需的行。
I would also suggest rewriting the assignment of values to min
and max
, as they are not going to detect non-Double data, which looks like what is intended by calling Double.isNan()
. This article might be a good approach.
我还建议将值的赋值重写为min和max,因为它们不会检测非Double数据,这看起来像调用Double.isNan()的目的。这篇文章可能是一个很好的方法。
#1
1
As has been mentioned, you need to initialize your variables, rather than just declaring them. I would highly suggest, from the nature of the comment regarding initializing the variable before the value it will be assigned, that you look at some of the code tutorials that can be found free on the internet. This will give you a better understanding of the basics of the Java language.
如前所述,您需要初始化变量,而不是仅仅声明它们。我强烈建议,从关于在分配值之前初始化变量的注释的性质,你看一些可以在互联网上免费找到的代码教程。这将使您更好地理解Java语言的基础知识。
With that being said, the compiler is directly telling you that there is an issue, and is telling you exactly what it is. Initialize your doubles, and recompile; the errors will indeed go away. If you try to compile prior to making these changes, it is likely you are seeing something similar to this:
话虽如此,编译器直接告诉你存在问题,并且正在告诉你它究竟是什么。初始化你的双打,然后重新编译;错误确实会消失。如果您在进行这些更改之前尝试编译,很可能您会看到与此类似的内容:
Somefile.java:123:error: variable min might not have been initialized
Somefile.java:123:error:变量min可能尚未初始化
min = (Double.isNan(min) || ...
min =(Double.isNan(min)|| ...
Changing this declaration
改变这个声明
double min;
double max;
to instead both declare and initialize the variables
而是声明和初始化变量
double min = 0.00;
double max = 0.00;
will resolve the specific compiler error you mentioned.
将解决您提到的特定编译器错误。
With that being said, there are some other issues that you will face with the implementation that you have. If you know that after a certain point, token, or other symbol within your input file you will not care about any other data, it is suggestible to restrict the lines being read to only those necessary to perform the work.
话虽如此,您将面临一些其他问题,您将面临实施。如果您知道在输入文件中的某个点,令牌或其他符号之后您不关心任何其他数据,则可以将读取的行限制为仅执行工作所需的行。
I would also suggest rewriting the assignment of values to min
and max
, as they are not going to detect non-Double data, which looks like what is intended by calling Double.isNan()
. This article might be a good approach.
我还建议将值的赋值重写为min和max,因为它们不会检测非Double数据,这看起来像调用Double.isNan()的目的。这篇文章可能是一个很好的方法。