+ 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
24 Réponses
+ 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.
+ 10
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ!
because if the string is empty you can call sc.nextLine() again, to catch the real string input.
+ 7
Jaya krishna You are right. So this maybe a bug.
+ 5
Jaya krishna So what about this input
1000
abcd
+ 4
Thanks everyone. I got the problem. It's happening with nextLine() method but if we will take next() method it's working fine.
+ 4
marjel101 If str already blank then why this condition?
+ 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
+ 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'.
+ 3
Hr Hridoy and Sasmit Waghmare Wrong place.
And Hr Hridoy this is second time after warning.
+ 2
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ!
Yes. But it actually because of the methods you used...
And same it is for above input you mentioned....
+ 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();
+ 2
marjel101 means that,
It works for both input type inputs, inline and separate lines..
A logic.
+ 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.
+ 2
Hr Hridoy You are at wrong place.
+ 2
Dejos Dowo If you don't know then don't say anything anywhere.
+ 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);
}
}
+ 1
sc.nextline(); 🔄sc.next();
+ 1
king Clash Riedznan Thanks but Already got Answer.
+ 1
الاساطير -AR
What is this?
+ 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.