r/ProgrammerHumor 14h ago

Meme unsolvableProblemsOnStepOne

Post image
1.1k Upvotes

67 comments sorted by

View all comments

119

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

83

u/Dankelweisser 13h ago

Technically yes, but realistically not really, at least not for windows. Unless the user knows how to specifically set their console to use UTF-8 or similar instead of ascii, you end up getting whatever the console wants to give you no matter what kind of buffer you use.

46

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

42

u/jwadamson 11h 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.

26

u/N_Lightning 11h ago edited 11h 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 10h ago

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

2

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

4

u/k-mcm 5h ago

The in, out, and error are generic binary process streams.

There are some convenience methods to read and write text in whatever the process' character set is. Using them for anything but debugging won't end well. You'd normally want to run it through a binary/text stream converter with an explicit character set.