I have to write a program to censor a string. Whenever I run the program, it works fine the first time, then stops progressing the second time after entering the bad word. I don't get what the problem is. PLease help:
java.util.Scanner;
public class stringCensorRunner {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int menuChoice = 1;
stringCensor theCensor = new stringCensor(); // a new stringCensor is constructed
String censored = ""; // censored is initialized to a null string
System.out.print("Enter your string: "); // user prompted to enter the string
String whatTheUserPut = input.nextLine();
theCensor.userString(whatTheUserPut);
//System.out.println("\nThe string is: " + whatTheUserPut);
//System.out.println("This string has " + theCensor.length() + " characters");
while (menuChoice != 2)
{
if (menuChoice == 1)
{
System.out.println("\nThe string is: " + whatTheUserPut);
// the user enters the bad word
System.out.print("Enter the bad word: ");
String theWord = input.nextLine();
theCensor.bannedWord(theWord);
// the length of the bad word is computed
int lengthOfBadWord = theWord.length();
String banned = "";
// a loop to print the required number of stars
// to cover up the bad word
for(int i = 1; i <= lengthOfBadWord; i++)
{
banned = banned + "*";
}
// System.out.println("\nbefore while statement");
// the bad word is replaced with stars
while (whatTheUserPut.indexOf(theWord) >= 0)
{
int aBegin = 0;
// if int aEnd is put outside, then the word not found function would work
int aEnd = whatTheUserPut.indexOf(theWord);
int bBegin = aEnd + lengthOfBadWord;
int bEnd = theCensor.length();
// the program would quit if aEnd < 0
if(aEnd < 0)
{
System.out.println("The word \"" + theWord + "\" was not found");
break;
}
// the program would compute the string replacing the bad word
else if (aEnd >= 0)
{
String aString = whatTheUserPut.substring(aBegin, aEnd);
String bString = whatTheUserPut.substring(bBegin, bEnd);
censored = (aString + banned + bString);
whatTheUserPut = censored;
}
else
System.out.println("just something");
}
System.out.println("The censored string is: " + censored);
}
else if (menuChoice == 2)
{
System.out.println("You have chosen 2, program terminating");
}
else
{
System.out.println("Invalid option");
}
System.out.print("Enter 1 to continue, 2 to quit: ");
menuChoice = input.nextInt();
System.out.println("You chose " + menuChoice);
}
}
}