Reputation: 384
I have a do while loop, and it is not working. As you can see in my code it is supposed to loop if the variable "choice" is not equal to p or n. even if I make choice equal p or n it loops at least once. Do I have bad placement or something?
do{
System.out.println("Enter the name of the contact you would like to update");
name = sc.nextLine();
System.out.println("Would you like to update their name(press n) or phone"
+ "number(press p)?");
choice = sc.nextLine();
if(choice.equalsIgnoreCase("n")){
System.out.println("Enter the new name for the contact");
String name1 = sc.nextLine();
SQL.updateName(name, name1);
c = SQL.searchByName(name1);
if(c != null)
System.out.printf("Name: %s\t Phonenumber: %s\n", c.getName(), c.getPhone());
else
System.out.println(name+" does not exist");
}
else if(choice.equalsIgnoreCase("p")){
System.out.println("Enter the new number for "+name);
phone = sc.nextLine();
SQL.updateNumber(name, phone);
c = SQL.searchByPhone(phone);
if(c != null)
System.out.printf("Name: %s\t Phonenumber: %s\n", c.getName(), c.getPhone());
else
System.out.println(name+" does not exist");
}
}
while(choice.equalsIgnoreCase("p") == false || choice.equalsIgnoreCase("n") == false);
Upvotes: 0
Views: 65
Reputation: 178333
Your logic is incorrect; any choice
will always be either not equal to "p"
or not equal to "n"
. You want to loop only if it's not "p"
and it's not "n"
. Use &&
:
while(choice.equalsIgnoreCase("p") == false && choice.equalsIgnoreCase("n") == false);
Upvotes: 4