java - printing 2 times donno why -
i having problem when break out remove part code prints main menu options twice
there no error in code , runs why print twice instead of once?
public void mainloop() { (int = 0; < 100; i++) { string x; system.out.println("please select option"); system.out.println("............................"); system.out.println("1 add name , number \n2 remove name , number \n3 search name , number \n0 exit"); system.out.println("............................"); x = input.nextline(); if (x.equalsignorecase("0")) { system.out.println("thank you!"); break; } if (x.equalsignorecase("1")) { string name; string number; system.out.println("please enter name below"); name = input.nextline(); system.out.println("please enter number below"); number = input.nextline(); system.out.println(""); system.out.println("your name " + name + " , number " + number + " has been saved!\n"); objectclass objectclassobject = new objectclass(name, number); test.add(objectclassobject); } if (x.equalsignorecase("2")) { system.out.println("-------all name , numbers-------"); system.out.println(""); (int j = 0; j < test.size();) { objectclass p = test.get(j++); system.out.println(j + ". name: " + p.getname() + " - " + p.getnumber()); } (int j = 0; j < test.size(); j++) { system.out.println(""); system.out.println("enter index number remove contact phonebook!"); int v = input.nextint(); int temp = (v - 1); if (v >= 1 && v <= test.size()) { system.out.println("name: " + test.get(temp).getname() + " , number: " + test.get(temp).getnumber() + " has been removed!!"); system.out.println(""); test.remove(temp); } else { system.out.println("please enter number properly!!"); } break; } } if (x.equalsignorecase("3")) { string y; system.out.println("*** enter name below search ***"); y = input.nextline(); (objectclass p : test) { string z = p.getname(); if (z.equalsignorecase(y)) { system.out.println("your name is: " + p.getname() + "\nyour number is: " + p.getnumber()); system.out.println(""); } if (!z.equalsignorecase(y)) { system.out.println("contact not found!!!"); } } } } }
}
system.out.println("please select option"); system.out.println("............................"); system.out.println("1 add name , number \n2 remove name , number \n3 search name , number \n0 exit"); system.out.println("............................");
this prints twice :/~
if prints twice, went 2 times thru loop. try display variable 'x' after reading it. bet have empty strings between legitimate input.
what happens nextint() doesn't consume newline. therefore, next time read x read end of line after value v.
Comments
Post a Comment