java - Scanner reading inputs but outputting previous inputs -


consider following snippet:

    system.out.print("input iteration cycles");     scanner reader=new scanner(system.in);     int iteration = reader.nextint();     system.out.print("input choice: var or par");     string choice=reader.nextline();     system.out.print(choice); 

i hoping 1 line prints first input , next print out print next input either var or par, seems second print prints out second input in addition first input. idea why?

nextint() not advance next line. must make explicit call nextline() after it. otherwise, choice given newline. add line code:

system.out.print("input iteration cycles"); scanner reader=new scanner(system.in); int iteration = reader.nextint(); reader.nextline(); //<==add system.out.print("input choice: var or par"); string choice=reader.nextline(); system.out.print(choice); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -