java - Printing a string input from scanner with leading whitespaces -


i have input scanned of int type , trying take second input of string type has whitespaces @ beginning, , while printing both inputs, want second 1 printed (with white spaces).

class input{     public static void main (string[] args) throws java.lang.exception{          scanner sc=new scanner(system.in);         int n=sc.nextint();           string s=sc.next();         s=s+sc.nextline();          system.out.println(n);         system.out.println(s);     } } 

input:

4       hello world. 

expected output:

4       hello world. 

actual output:

4 hello world. 

scanner.next() uses default delimiter, capture whitespace sequence. use scanner.nextline() instead:

int n = sc.nextint(); sc.nextline();  // consume newline string s = sc.nextline(); 

ideone demo


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -