dimanche 15 avril 2018

Java while loop sentinel issue

My while loop runs fine and the sentinel (String End) value ends the loop...the issue is that the when the sentinel is entered, the else println statement occurs.

I want the sentinel value to end the loop and not have the final else statement occur

while (!nameSearch.equalsIgnoreCase(sentinel)) {

  showMenu();
  nameSearch = keyboard.nextLine();

  if (girlList.contains(nameSearch.toLowerCase()) && 
     boyList.contains(nameSearch.toLowerCase())) {
     System.out.println("Name found in both boy and girl names\n");
   } else if (boyList.contains(nameSearch.toLowerCase())) {
        System.out.println("Boy name found");
   } else if (girlList.contains(nameSearch.toLowerCase())) {
         System.out.println("Girl name found");
   } else {
         System.out.println("Name not found");
   }

}

System.out.println("program end");

Aucun commentaire:

Enregistrer un commentaire