+ 1
can anyone tell me why the code below named 1 java 1 is not working properly... while the code named java 2 is working fine??
in both codes..my motive is to read and display a char and a string..... in code 1 i first read a char and string which is not working properly..... but in code 2 i read and display a string first and a char next which is working fine... can i know the reason please why code 1 is working.. https://code.sololearn.com/cRqjm4d3n6xb/?ref=app https://code.sololearn.com/ce5BYIOL1L58/?ref=app
5 Antworten
+ 2
i know the difference... it seems like u don't get my point..... please view the two codes again
+ 2
No.... i compiled both codes in my laptop using jdk...... it gives same output as sololearn's output... Hope you will got my point.
0
check out the difference between .read() and .readLine() and you'll understand
0
read() reads a single character, if you enter one character in one line and then something else in the second line of sololearns input prompt, read() reads the first letter leaving a new line character which gets read by readLine() because it reads until the new line character - in this case just this
so your code works as it should be
0
i cant tell you more then that both codes work as they should.
reading a char first leaves the end of line sign in the stream which then gets read by readline() leaving whatever you entered as wanted string in the stream.
to fix it you can simply call readline() after you read your character.