title: string index out of bounds exception, on an if else statement
body: <p>The problem code is below, if you need the entire main method to help me, please ask. The code complies but does not run as expected. I am trying to make the code report back an exclamation mark if the number is out of bounds/larger than the last position of the source text, which is a string the user inputs, so the length cannot be predefined. Exception is 'StringIndexOutOfBoundsException'</p>
  
  <p>TDLR num is an int, sourcetext is a string, both are inputs. Exception: when code should output an '!' instead. </p>
  
  <pre><code>import java.util.Scanner; 
  
  public class Temp {
  
      public static void main(String[] args) { 
  
          Scanner sc;
          int  result, num= 0, end = -2, temp, infolost, count;
          String word, sourcetext, answer, space= " ";
          String sourcetext2, temp2;
          char input, result2, chalost;
          sc = new Scanner(System.in);
  
          System.out.println("please enter sourcetext");
          sourcetext = sc.nextLine(); // user inputs source text
          sourcetext = sourcetext.toLowerCase(); // converts sourcetext into lowercase                      
          System.out.print("Would you like to 1 encrypt, or 2 decrypt?");
          answer = sc.next(); // user inputs choice
  
          if (answer.equals("1")||(answer.equals("encrypt"))) {
  
              System.out.println("Please enter at least one word to encrypt");
              word = sc.next();   // user inputs one word
  
              for (int i= 0; i &lt; word.length(); i++) { 
                  temp = sourcetext.indexOf(word.charAt(i)); // uses index to convert char  positions int num
                  System.out.print(space + temp + space);          
              }
              System.out.print(space + end); 
          }
          else if (answer.equals("2")||(answer.equals("decrypt"))) {
  
              System.out.println("Please enter digits, with one space between each. End  with -2");                 
              while (num &gt; -2) {  
                  num = sc.nextInt(); // num to decrypt
                  if (num &gt; -2) {      
                      result2 = sourcetext.charAt(num); // num converted into characters
                      System.out.print(result2);
                  } else if (num &gt; sourcetext.length()) {
                       System.out.print("!");
                  } else if (num&lt;0) {
                       System.out.print("end");
                  }
              }
          }
      }
  }
  </code></pre>