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();
Comments
Post a Comment