java - Why cant I input info after if statement? No errors show -
how can program ask input using scanner after if statement?
import java.util.scanner; public class app1 { public static void main(string[] args) { scanner keyboard = new scanner(system.in); string gender; string fname; string lname; int age; string female = "f"; string male = "m"; string = ""; string gettingmarried = "y"; string notgettingmarried = "n"; system.out.println("what gender (m or f)"); gender = keyboard.nextline(); system.out.println("what first name?"); fname = keyboard.nextline(); system.out.println("what last name?"); lname = keyboard.nextline(); system.out.println("age:"); age = keyboard.nextint(); system.out.println(); if (gender.equals(female) && age >= 20) { system.out.println("are married " + fname + " (y or n?)"); = keyboard.nextline(); } else if (gender.equals(male) && age >= 20) { system.out.println("are married" + fname + "(y or n?)"); = keyboard.nextline(); } } }
it because use nextint()
takes int
value , not full line. when try again read keyboard.nextline()
catching it's rest of sentence of line in put int
.
change system.out.println();
before if
statement keyboard.nextline();
edit: if want more extended explanation why have put keyboard.nextline();
after keyboard.nextint()
can consult answer in question same problem: why isn't scanner input working?
i expect helpful you!
Comments
Post a Comment