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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -