hi, i just learned how to use the while code and put it in a simple program im working on, i however am stumped on how to use it in a part of it here is a snippet of the program code

String classname, faction;
Scanner inputDevice = new Scanner(System.in);
System.out.println("Welcome");
System.out.println("This program will help you decide which World of Warcraft");
System.out.println("faction and class is best for you.");
System.out.println("");
System.out.println("Please enter a faction, horde or alliance.");
faction = inputDevice.nextLine();
if(faction.equalsIgnoreCase("horde"))
{
System.out.println("You have chosen the horde.");
System.out.println("");
System.out.println("There are 9 different character classes to choose from");
System.out.println("A hunter, warrior, warlock, druid, paladin, mage, shaman, and priest.");
while (faction.equalsIgnoreCase("horde"))
{
System.out.println("");
System.out.println("Please enter a WoW class now.");
classname = inputDevice.nextLine();
if(classname.equalsIgnoreCase("rogue"))
{
System.out.println("A rogue uses stealth and daggers to destroy it's opponent.");
System.out.println("");
}
else if(classname.equalsIgnoreCase("hunter"))
{

you can see where i have a while statment looping.
it loops after the user chooses a faction(horde or alliance)
i want to put a loop in so the faction part loops as well so the program doesn't end if they enter a typo. Thanks!

This will loop over and over till either word is typed correctly

System.out.println("Welcome");
System.out.println("This program will help you decide which World of Warcraft");
System.out.println("faction and class is best for you.");
System.out.println("");

do{
   System.out.println("Please enter a faction, horde or alliance.");
   faction = inputDevice.nextLine();
}while(faction.equalsIgnoreCase("horde") || faction.equalsIgnoreCase("alliance"));

PS: Your while loop on line 15 will create infinite loop so you better sort out that logic

This article has been dead for over six months. Start a new discussion instead.