Reputation: 645
I wrote this code which is apparently right. The problem is that the variable temp is always null. On the first cycle the program should enters in the second statement, but this does not happens. Why?
ArrayList<Pair<Float, Float>> tmp = new ArrayList<>();
for (int i=0; (line = reader.readLine()) != null; i++) {
Float temp = null;
if (line.equals("----")) {
i = 0;
points.add(tmp);
tmp = new ArrayList<>();
} else if (i%2 == 0) {
temp = Float.parseFloat(line);
} else {
tmp.add(new Pair<Float, Float>(temp, Float.parseFloat(line)));
System.out.println("LINE: "+tmp.get(0).first);
}
}
PS: even the IDE says that the values temp will be always null
Upvotes: 0
Views: 396
Reputation: 652
The value is temp is not always null. If you print the value of temp just after parsing it, you will see this.
temp = Float.parseFloat(line);
System.out.println("temp: " + temp);
If you intend to use the value of temp from one iteration during the next iteration, it would be wise of you not to set the variable to null every time.
Upvotes: 0
Reputation: 35795
You declare Float temp = null;
inside your for
loop. So it will always be set to null
when an iteration of your loop starts. Even if you hit the second statement, temp
will get a value but then is immediately discarded and created anew with value null
.
Try to declare Float temp = null
before the loop.
Upvotes: 4