0

why it is not taking String input ?

int i = scan.nextInt(); double d=scan.nextDouble(); String s=scan.nextLine(); System.out.println("String: " + s); System.out.println("Double: " + d); System.out.println("Int: " + i);

17th May 2017, 8:05 PM
Somnath Ghosh
Somnath Ghosh - avatar
6 Respostas
+ 16
tldr: Change the input order, so that next line isn't the last called method. Further infornation: http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo
17th May 2017, 8:22 PM
Tashi N
Tashi N - avatar
+ 15
The ENTER user presses after int/double (non-string) input, is considered as the next String input. You can change the order as Tashi suggested, or put one extra scan.nextLine() to consume the Enter. https://code.sololearn.com/cNgcAvH5PnM9/?ref=app
17th May 2017, 8:34 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 13
It's said in the source I linked.
17th May 2017, 8:32 PM
Tashi N
Tashi N - avatar
+ 10
What isn't taking string input? Code playground? Could you post the full example code?
17th May 2017, 8:06 PM
Tashi N
Tashi N - avatar
0
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i = scan.nextInt(); double d=scan.nextDouble(); String s=scan.nextLine(); System.out.println("String: " + s); System.out.println("Double: " + d); System.out.println("Int: " + i); } }
17th May 2017, 8:12 PM
Somnath Ghosh
Somnath Ghosh - avatar
0
i know the solution but why it is not printing...
17th May 2017, 8:31 PM
Somnath Ghosh
Somnath Ghosh - avatar