+ 1

Why space is not accepted in typing input in java ?

when I type a space in input , it is not accepted as a character by the computer . Whatever I type after space becomes invalid . Why is it so? . What is the solution for it ?

22nd Dec 2018, 3:21 PM
Ashwanth K
Ashwanth K - avatar
4 Respuestas
+ 13
Hm, hard to tell without reviewing your code... Could you provide a minimal working example that shows the problem? Would be best to publish it on the code playground and link it here.
22nd Dec 2018, 4:34 PM
Tashi N
Tashi N - avatar
+ 10
Thank you. The error is raised because the class Scanner is from the util package and needs to be imported (java.lang classes don't need to be imported). The next() method of the Scanner class reads one token, in your code the first input String. A whitespace is a delimiter (new String is coming up next). What you can do: 1. Just invoke next() again, it will read the second token then: String str = input.next().toString(); System.out.println(str); 2. Read the full line in one go -> nextLine() method instead of next() and parse it afterwards.
22nd Dec 2018, 5:41 PM
Tashi N
Tashi N - avatar
+ 2
use input.nextLine() instead of input.next()
22nd Dec 2018, 8:08 PM
Rishi Anand
Rishi Anand - avatar
22nd Dec 2018, 5:30 PM
Ashwanth K
Ashwanth K - avatar