Skip to main content

Creating a loop that only include parts of code

1 reply [Last post]
pja2848
Offline
Joined: 2012-08-11
Points: 0

I'm trying to create a loop that only loops parts of the code but I cannot figure out the right coding. When choice is "y", the application runs from the beginning. When choice is "n", display "Bye - Come back soon!" and ends the application. When there is no entry, display "Error! This entry is required. Try again." and loop back to [System.out.print("Try again? (y/n)");]. When choice is other than y and n, display "Error! Entry must be 'y' or 'n'. Try again." and loop back to [System.out.print("Try again? (y/n)");].
Y and N choice works the way I intented to but I cannot make no entry and other entry to loop back to try again quesiton line. Please help
Following is what I have currently

//see if the user wants to continue
System.out.print("Try again? (y/n)");
choice = sc.next();
System.out.println();
//only accept y or n, error message for other choices           
while (!choice.equalsIgnoreCase("y"))
   {
      if (choice.equalsIgnoreCase("n"))
         {
          System.out.println("Bye - Come back soon!");
          break;
          }
      //no entry
      else if (choice == null)                 
          {
          System.out.println("Error! This entry is required. Try again.");
          }
     //entry other than y and n
     else
          {
          System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
          }               
    }      

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joergwassmer
Offline
Joined: 2004-09-20
Points: 0

Your question is your answer:
"I cannot make no entry and other entry to loop back to try again quesiton line".