title: Replace all occurrences of the word "java" with uppercase "JAVA" without using regex or replace method
body: <p>I have an assignment to replace all occurrences of the word "java" with uppercase "JAVA" without using regex or replace method. Please see my code &amp; output below. Please confirm if this is the best way to do that.</p>
  
  <p>Code:</p>
  
  <hr>
  
  <pre><code>import java.util.Scanner;
  public class Substitute
  {
      public static void main(String[] args)
      {
          String javaString = "java";
          String left, right, sub = "", result = "";
          int index;
          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(!(sentence.equalsIgnoreCase("quit")))
          {   
              index = sentence.toLowerCase().indexOf("java");
              if(index != -1)
              {
                  if(javaString.equalsIgnoreCase(sentence.substring(index, index + 4)))
                  {
                      while(index != -1) 
                      {
                          left = sentence.substring(index, index + 4);
                          right = sentence.substring(index + 4);
  
                          if(sentence.indexOf(right) &lt;= 0 || sentence.indexOf(right) &gt;= sentence.length())
                          {
                              sub = sentence.substring(index, index + 4);
                              sub = sub.toUpperCase();
                              if(result == "")
                              {
                                  result = sentence.substring(0, index) + sub;
                              }
                              else
                              {
                                  result = result.substring(0, index) + sub;
                              }
                              index = -1;
                          }
                          else if(sentence.indexOf(right) &gt; 0 &amp;&amp; sentence.indexOf(right) &lt; sentence.length())
                          {
                              sub = sentence.substring(sentence.indexOf(left, index), sentence.indexOf(right, index));
                              sub = sub.toUpperCase();
                              if(result == "")
                              {
                                  result = sentence.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index));
                              }
                              else
                              {
                                  result = result.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index));
                              }
                              index = sentence.toLowerCase().indexOf("java", index + 4);
                          }
                          if(index == -1)
                          {
                              System.out.println(result);
                          }
                      } //end of while loop
                      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.toLowerCase().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.toLowerCase().indexOf("java");
                  result ="";
              }   
          }
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <hr>
  
  <pre><code>$ java Substitute
  Enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Program
  There is no word "java" in the sentence.
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogram java
  rogram JAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: s java and java
  s JAVA and JAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: ScriptJava
  ScriptJAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: p java
  p JAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: scjava
  scJAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Javascript
  JAVAscript
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Programming java and java JaVa
  Programming JAVA and JAVA JAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogramming java and java JaVa
  rogramming JAVA and JAVA JAVA
  
  Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: quit
  </code></pre>