Different output for "run" and "debug" when using readline ()

I get two different outputs for the same code. I get one result when I debug and scan each line (using Netbeans 8.1). And I get a different result when I run the code right away.

This is the code

public class Testing { public static void main(String... args) throws IOException { BufferedReader file = new BufferedReader(new FileReader("input")); String str = file.readLine(); System.out.println(str); } } 

This is the input file.

 first second third fourth 

In both cases, the code should print the first line of first . But this only happens when I run the code.

If I debug the code and go through each line, the second line of second is printed.

Why is this happening?

Update: Below is a screenshot of the debugging screen. Right now, if I step over, it will execute the line System.out.println. As you can see on the right, str contains "second".

enter image description here

+7
java netbeans
source share
1 answer

Does your environment check ID file.readLine() during debugging? In Eclipse, you can define "watchful expressions" that do just that.

This may explain your problem, because when you go through the code line by line, there is exactly one line in which file defined, and therefore the expression above can be evaluated. Therefore, the output of second instead of first .

Update: now that you have added the screenshot, its clarity. This is partly due to a mistake made by Netbean developers because they chose "Variables" in the header, which is misleading because evaluating variables does not have a side effect, although evaluating arbitrary expressions (e.g. file.readLine() ) clearly has.

+11
source share

All Articles