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?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

10th Dec 2017, 3:39 PM
SubbaReddy G
SubbaReddy G - avatar
5 Answers
+ 2
i know the difference... it seems like u don't get my point..... please view the two codes again
10th Dec 2017, 4:56 PM
SubbaReddy G
SubbaReddy G - avatar
+ 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.
11th Dec 2017, 2:25 AM
SubbaReddy G
SubbaReddy G - avatar
0
check out the difference between .read() and .readLine() and you'll understand
10th Dec 2017, 4:07 PM
Jeremy
Jeremy - avatar
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
10th Dec 2017, 5:03 PM
Jeremy
Jeremy - avatar
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.
11th Dec 2017, 7:43 AM
Jeremy
Jeremy - avatar