-1

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.

saybrook
  • 1
  • 2
  • You need to use `String.split` to split your line into parts – Scary Wombat May 31 '17 at 02:42
  • Great, thanks, I'll give it a shot – saybrook May 31 '17 at 02:58
  • "Declared" is not "initialised". Initialisation involves giving it a value, you didn't give it a value, and trying to read its value before initialisation doesn't make much sense. You also should probably declare them outside of the loop, otherwise they will reset every iteration. – Bernhard Barker May 31 '17 at 06:46
  • Duplicate of [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error). – Bernhard Barker May 31 '17 at 06:47
  • Hmmm... thanks for your reply Dukeling, but if I'm trying to declare a double that I later want to assign as the minimum and maximum of a line, does it make sense to assign it before I run that code below it? – saybrook May 31 '17 at 19:25

1 Answers1

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.

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

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.

Community
  • 1
  • 1