Assignment:
Create a program Substitute that gets a sentence from the user and replaces every occurrence of the word java (uppercase, lowercase, or a combination) with JAVA. You may not change the original content of the sentence except for the substitution of JAVA. Note that Java could appear in a larger word such as JavaScript, openjava, javazon, Ajjavara, Javanese. You MUST use the substring method to do this. Display the modified sentence. The program must keep running until the user enters "quit" (upper- or lowercase). I cannot use replace method or regex for this program.
Code:
import java.util.Scanner;
public class Substitute
{
public static void main(String[] args)
{
System.out.println("Program written by Eric Wessel");
System.out.println("*******************************");
System.out.println("Program that reads in a sentence and replaces every occurrence of the word \"java\" with \"JAVA\" in the sentence.");
System.out.println("************************************************************************************************************");
// Declare and initialize variables
String left, right, sub = "", result = "";
int index, indexOfRight;
// Prompt for a sentence with a word "java" and store it in sentence String variable
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
String sentence = input.nextLine();
// While loop 1 for the program to keep running until the user enters quit.
while(!(sentence.equalsIgnoreCase("quit")))
{
// Find the index of first java occurrence after converting sentence to uppercase.
index = sentence.toUpperCase().indexOf("JAVA");
// If there is a word "java" in the sentence, then separates left and right parts of word java, converts the word java to
// uppercase and store it in sub and then concatenates in the order: left + sub + right. Otherwise it will print:
// "There is no word "java" in the sentence" and prompt for next sentence.
if(index != -1)
{
// While loop 2 to continue until index returns -1 (or no more word java in the sentence).
while(index != -1)
{
left = sentence.substring(index, index + 4);
right = sentence.substring(index + 4);
indexOfRight = sentence.indexOf(right, index + 4);
if(indexOfRight <= 0 || indexOfRight >= sentence.length())
{
sub = sentence.toUpperCase().substring(index, index + 4);
if(result == "")
{
result = sentence.substring(0, index) + sub;
}
else
{
result = result.substring(0, index) + sub;
}
index = -1;
}
else if(indexOfRight > 0 && indexOfRight < sentence.length())
{
sub = sentence.toUpperCase().substring(sentence.indexOf(left, index), sentence.indexOf(right, index + 4));
if(result == "")
{
result = sentence.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index + 4));
}
else
{
result = result.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index + 4));
}
index = sentence.toUpperCase().indexOf("JAVA", index + 4);
}
if(index == -1)
{
System.out.println(result);
}
} // end of while loop 2
System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
sentence = input.nextLine();
index = sentence.toUpperCase().indexOf("JAVA");
result = "";
}
else
{
System.out.println("There is no word \"java\" in the sentence.");
System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
sentence = input.nextLine();
index = sentence.toUpperCase().indexOf("JAVA");
result ="";
}
} // end of while loop 1
}
}
Output:
$ java Substitute
Program written by Eric Wessel
*******************************
Program that reads in a sentence and replaces every occurrence of the word "java" with "JAVA" in the sentence.
************************************************************************************************************
Enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: javaScript, openjava, javazon, Ajjavara, JavaneseJavaScript, openjava, javazon, Ajjavara, Javanese
JAVAScript, openJAVA, JAVAzon, AjJAVAra, JAVAneseJAVAScript, openJAVA, JAVAzon, AjJAVAra, JAVAnese
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: javajava
JAVAJAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: quit