- 1

I have variables does not take the value entered !!

I wrote the code in java and wanted to give values ​​to variables I have String str1 int int1 String str2 And using class Scanner for input but str2 does not take a value !!! why ? Check it out via the code below https://code.sololearn.com/c6lYpCd4VjBA

15th Sep 2021, 9:47 PM
Saoula Hocine
Saoula Hocine - avatar
1 ответ
+ 2
That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline. int int1= cl.nextInt(); cl.nextLine(); // Consume newline left-over String str2= cl.nextLine(); or Instead of input.nextLine() use input.next(), that should solve the problem. Keep learning & happy coding :D
15th Sep 2021, 10:21 PM
SoloProg
SoloProg - avatar