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
I have written this code to parse the top two lines, as well as the other data, except for the placeholder value %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.
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.