G
G
genbachae2020-02-16 05:01:16
Java
genbachae, 2020-02-16 05:01:16

Why is the code not being executed after the while loop?

I run the code under the debugger:

import java.io.*;

public class Program {

    public static void main(String[] args) throws IOException {
        char[] buf;
        buf = new char[9];
        BufferedInputStream bf = new BufferedInputStream(System.in);

        try{
            int i, j, el;
            j = bf.available();
            char c;
            while(((i = bf.read()) != -1) & j > 0) {
                c = (char) i;
                j = bf.available();
                buf[j] = c;
            }
            el = Integer.parseInt(String.copyValueOf(buf));
            System.out.println(el);
        }
        finally{
            bf.close();
        }
    }
}

in the console I type for example: "123", why is the code below never executed?:

el = Integer.parseInt(String.copyValueOf(buf));

the while loop is executed without problems, and then nothing happens.

Why is the block of code below not being executed?

el = Integer.parseInt(String.copyValueOf(buf));
            System.out.println(el);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2020-02-16
@genbachae

And in the loop condition, do you definitely need a bitwise AND (&), and not a logical (&&)? while(((i = bf.read()) != -1) & j > 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question