title: JAVA - String index out of range, I can't figure out why
body: <p>So I'm making a program that decodes a coded message, it compiles but when I run it I get a java.lang.StringIndexOutOfBoundsException: String index out of range: 1 error and I can't figure out where this is coming from.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>    import java.util.Scanner;
  
  public class ReverseCodeProgram {
    public static int i;
  
    public static String decodeLetter(String s){
        String a = "";
        if ((s.charAt(0) == '.'))
        a = "E";
        if ((s.charAt(0) == '-'))
        a = "T";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '-'))
        a = "M";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.'))
        a = "N";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.'))
        a = "I";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-'))
        a = "A";
        if ((s.charAt(0) == ' ') &amp;&amp; (s.charAt(1) == ' '))
        a = " ";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '.'))
        a = "R";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.'))
        a = "S";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-'))
        a = "U";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.'))
        a = "D"; 
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '.'))
        a = "G";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-'))
        a = "K";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-'))
        a = "O";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '-'))
        a = "W";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '.'))
        a = "B";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-') &amp;&amp; (s.charAt(3) == '.'))
        a = "C";    
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-') &amp;&amp; (s.charAt(3) == '.'))
        a = "F";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '.'))
        a = "H";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '-') &amp;&amp; (s.charAt(3) == '-'))
        a = "J";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '.'))
        a = "L";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '-') &amp;&amp; (s.charAt(3) == '.'))
        a = "P";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '-'))
        a = "Q";
        if ((s.charAt(0) == '.') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '-'))
        a = "V";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '-'))
        a = "X";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '.') &amp;&amp; (s.charAt(2) == '-') &amp;&amp; (s.charAt(3) == '-'))
        a = "Y";
        if ((s.charAt(0) == '-') &amp;&amp; (s.charAt(1) == '-') &amp;&amp; (s.charAt(2) == '.') &amp;&amp; (s.charAt(3) == '.'))
        a = "Z";
        s = a;
        return s; 
    }
    public static void main(String[] args) {
      System.out.println("Please enter the sentence in Morse code");
      String code = new Scanner(System.in).nextLine();
      String decodedCharacter = "", character = "", decodedCode = "";
      for (i = 0; i &lt;  code.length(); i++){
        if (code.charAt(i) == ' '){
          for (int j = i - 4; j &lt; i; j++){        
          character += code.charAt(j); 
          decodedCharacter = "" + decodeLetter(character);
        }
        decodedCode += decodedCharacter;
       }
  
      }
      System.out.println(decodedCode);    
    }
  }
  </code></pre>