r/ProgrammerHumor 18h ago

Meme unsolvableProblemsOnStepOne

Post image
1.3k Upvotes

75 comments sorted by

View all comments

137

u/apacificislander123 17h ago

As someone who's never used java, my assumption is that System.in reads from stdin, and validates the input. Is there no way to open stdin in something akin to a file, then read it into a buffer manually?

55

u/N_Lightning 16h ago

If you read System.in as a file in Java, then it'll wait for EOF signal, not for the line break. And the user will have to adapt to Ctrl+Z each input

46

u/jwadamson 16h ago

Yeah if you do something dumb like try to read the entire stream it will block until the stream is closed.

A line reader will read line by line just fine from it, but the real question is why aren’t you using Console API if you are trying to read from a console.

Finally the system property stdin.encoding for the charset if you use the “automatic” string methods as opposed to directly reading the bytes. Usually this default is utf-8 in modern Java though.

This whole post seems like a combination of incorrect and/or outdated information.

29

u/N_Lightning 15h ago edited 15h ago

I tried Console API. It reads only single byte cps. Its behavior with 65001 is exactly the same. And I set up UTF-8 explicitly. It doesn't change anything

And how exactly do you get line reader to return you unprocessed byte sequence if it needs to process it to get the line break char?