+ 22

[Solved] Why this is happening?

I was making a simple code on Sololearn Playground then I got a weird issues. Can anyone explain me why this is happening? Here is my code. Check and tell me what is the issue. I am taking input in first code as:- abc 3 And in Second code as :- 3 abc But in second code string value not printing. Why? https://code.sololearn.com/cksbZafkq8FK/?ref=app https://code.sololearn.com/c5c6Z53HmsIN/?ref=app

14th Dec 2019, 1:01 PM
A͢J
A͢J - avatar
24 Antworten
+ 12
int num = sc.nextInt(); sc.nextLine(); // add this here String str = sc.nextLine(); The input stream contains a \n character left behind after reading the int, the `sc.nextLine();` will consume that leftover character, so the next call for `nextLine` will read the input correctly.
14th Dec 2019, 1:15 PM
Ipang
+ 10
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! because if the string is empty you can call sc.nextLine() again, to catch the real string input.
14th Dec 2019, 2:03 PM
marjel101
marjel101 - avatar
+ 7
Jaya krishna You are right. So this maybe a bug.
14th Dec 2019, 1:14 PM
A͢J
A͢J - avatar
+ 5
Jaya krishna So what about this input 1000 abcd
14th Dec 2019, 1:16 PM
A͢J
A͢J - avatar
+ 4
Thanks everyone. I got the problem. It's happening with nextLine() method but if we will take next() method it's working fine.
14th Dec 2019, 1:24 PM
A͢J
A͢J - avatar
+ 4
marjel101 If str already blank then why this condition?
14th Dec 2019, 1:37 PM
A͢J
A͢J - avatar
+ 4
Just add this line whenever you take int or double value from a user "sc.nextLine();" Because when you enter a number then press "enter". Now both string value and int collapsed... How come enter = string? Answer: "\n" that is how I learned lol... maybe it is wrong
15th Dec 2019, 3:50 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 3
sc.nextLine() reads string just after taking 3 by sc.nextInt(); Try as 3 abc Instead of 3 abc edit: here, nextInt() read the integer value, before a space or \n encounters. what ever after you will be read by sc.nextLine() ends with '\n'.
14th Dec 2019, 1:10 PM
Jayakrishna 🇮🇳
+ 3
Hr Hridoy and Sasmit Waghmare Wrong place. And Hr Hridoy this is second time after warning.
16th Dec 2019, 10:22 AM
A͢J
A͢J - avatar
+ 2
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! Yes. But it actually because of the methods you used... And same it is for above input you mentioned....
14th Dec 2019, 1:18 PM
Jayakrishna 🇮🇳
+ 2
You could also check for input on the same line or the next by adding this after the sc.nextLine(): if (str.equals("")) str = sc.nextLine();
14th Dec 2019, 1:33 PM
marjel101
marjel101 - avatar
+ 2
marjel101 means that, It works for both input type inputs, inline and separate lines.. A logic.
14th Dec 2019, 1:47 PM
Jayakrishna 🇮🇳
+ 2
Glêdson Seixas Camara Lée Coquet hdypebn Warning for you guys. Don't Spam here or in others questions. If you have valid answer then welcome.
15th Dec 2019, 7:04 AM
A͢J
A͢J - avatar
+ 2
Hr Hridoy You are at wrong place.
16th Dec 2019, 9:10 AM
A͢J
A͢J - avatar
+ 2
Dejos Dowo If you don't know then don't say anything anywhere.
16th Dec 2019, 9:11 AM
A͢J
A͢J - avatar
+ 1
Try this code. It works import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); int num = sc.nextInt(); System.out.println("String = " + str); System.out.println("Number = " + num); } }
15th Dec 2019, 7:14 AM
Riedznan
Riedznan - avatar
+ 1
sc.nextline(); 🔄sc.next();
15th Dec 2019, 1:35 PM
king Clash
king Clash - avatar
+ 1
king Clash Riedznan Thanks but Already got Answer.
15th Dec 2019, 1:39 PM
A͢J
A͢J - avatar
+ 1
الاساطير -AR What is this?
15th Dec 2019, 3:41 PM
A͢J
A͢J - avatar
+ 1
when same object of Scanner class is used the objects treat the input as STACK and as by nature as stack is LIFO so the last input becomes the next most recent output, so irrespective of any data type(i.e. separated by deli meters in the console) your output's gonna differ each time.
16th Dec 2019, 1:45 AM
Aditya
Aditya - avatar