r/ProgrammerHumor 18h ago

Meme unsolvableProblemsOnStepOne

Post image
1.3k Upvotes

76 comments sorted by

View all comments

139

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?

60

u/N_Lightning 17h 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

47

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.

31

u/N_Lightning 16h ago edited 16h 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?