error in taking input string after integer in java -
i trying :
int n = myscanner.nextint(); for(int i=0;i<n;i++){ string str = myscanner.nextline(); . . . }
when compiled shows errors java.util.scanner.nextint(scanner.java:2117). thought problem of nextline() used next() . found out if add myscanner.nextline() after taking input in n i.e
int n = myscanner.nextint(); myscanner.nextline();
then worked fine. want know why happened?
you need consume newline character enter when passing integer:
int n = myscanner.nextint(); //gets integers, no newline myscanner.nextline(); //reads newline string str; for(int i=0;i<n;i++){ str = myscanner.nextline(); //reads next input newline . . . }
Comments
Post a Comment