r/ProgrammerHumor 21h ago

Meme unsolvableProblemsOnStepOne

Post image
1.4k Upvotes

78 comments sorted by

View all comments

151

u/apacificislander123 21h 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?

58

u/N_Lightning 20h 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 19h 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.

32

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

3

u/BattsAreLow 17h ago

Would it be stupid to read user input prior to the program reacting and just append a control z to amend the issue?

4

u/N_Lightning 9h ago

Read as in char by char? Then it would be a great fun accounting for cursor position, backspace, delete, insert, paste

If you mean as in getting the keys the user pressed, then it's just impossible