0

Scanner sc = new Scanner(System.in); String s = sc.next();

Where I want my string to be 'This is a string' When I print it only return 'This' if suppose i gave the above input because I've used space. So what I have to do in such a way that I should not use for loop to initialize such a string.

vam
  • 352
  • 4
  • 14
  • It is not working. – vam Aug 01 '16 at 19:05
  • Did you use `nextInt` or other `nextXXX` method before it? – Pshemo Aug 01 '16 at 19:05
  • `import java.util.*; class A{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t= sc.nextInt(); for(int i=0;i – vam Aug 01 '16 at 19:06
  • Related [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](https://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Pshemo Aug 01 '16 at 19:06
  • Thanks @Pshemo ! It solved my issue – vam Aug 01 '16 at 19:11
  • That is common problem which everyone faced at least once :) To simplify it: when scanner is asking you for data and you do `123[enterKey]` you are putting into stream characters `123\r\n` (where `\r` and `\n` represent line separators). `nextInt()` doesn't consume line separators so it consumes only `123`. Now `nextLine` is suppose to return text between current position of cursor and next line separator (or end of string). Since it finds line separators right away it returns empty string which makes it look like it didn't work. – Pshemo Aug 01 '16 at 19:14

0 Answers0