Caution while taking input using Scanner in java | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 6

Caution while taking input using Scanner in java

Scanner sc=new Scanner (System.in); int a =sc.nextInt(); String s=sc.nextLine(); This code will skip sc.nextLine ,and ends only taking an integer as input, as it(.nextLine()) will consume the new line character (Enter) given by user. You can read in detail , why is it so:- https://stackoverflow.com/q/13102045/9190688

18th Mar 2018, 8:27 AM
Mr.Curious
Mr.Curious - avatar
2 Réponses
+ 15
thanks for the information //while making some codes , users encounter some errors due to this //I have seen posts on it
18th Mar 2018, 8:42 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
@ gaurav , its my pleasure
18th Mar 2018, 10:50 AM
Mr.Curious
Mr.Curious - avatar