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 -

c++11 - Intel compiler and "cannot have an in-class initializer" when using constexpr -

python - get cookie expiry time using the requests library -