+ 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 ?
4 ответов
+ 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.
+ 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.
+ 2
use input.nextLine() instead of input.next()