java - Scanner class skipping over code -



java - Scanner class skipping over code -

so have method isn't complicated @ all, seems skipping on 2 lines of code in while loop have. have tried do-while , normal while loop, neither of help. here code:

{ system.out.println("enter building material add together :: "); string first = keyboard.nextline(); system.out.println("how many of material add together ::"); int sec = keyboard.nextint(); int = (int)item.getid(first); //long section of code here. it's not necessary system.out.println(""); system.out.println("lumber - " + lumber); system.out.println("plywood - " + plywood); system.out.println("cinderblocks - " + cinder); system.out.println("mortar - " + mortar); system.out.println("tank traps - " + tanktrap); system.out.println("metal poles - " + metalpole); system.out.println(""); system.out.println("would add together more materials? (yes / no) :: "); string response = keyboard.next(); if(response.equalsignorecase("yes")) { wantsnext = true; }else{ wantsnext = false; } }while(wantsnext);

the output of follows :

welcome ogx_killer's epoch building supplies calculator come in building material add together :: cinderblock wall how many of material add together :: 10 lumber - 0 plywood - 0 cinderblocks - 70 mortar - 20 tank traps - 0 metal poles - 0 add together more materials? (yes / no) :: yes come in building material add together :: how many of material add together ::

as can see, sec time around in loop, skips on first input, leaving sec input answered, rendering loop useless. help appreciated. have feeling problem scanner method using. needs other keyboard.nextline(). many thanks!

problem:

string first = keyboard.nextline();

the problem consume new line "\n" of keyboard.next(); entered user skipping it.

solution:

use next() instead.

or

after phone call next() phone call nextline() consumed "\n"

sample:

system.out.println("would add together more materials? (yes / no) :: "); string response = keyboard.next(); keyboard.nextline();

java java.util.scanner

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -