java - Scanner reading inputs but outputting previous inputs -
this question has answer here:
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
Post a Comment