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

sql - Time in datatime field -

javascript - How to split the success output from an Ajax post? -

java - sharing object across different classes -