1

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.

Here's the 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) == '-') && (s.charAt(1) == '-'))
      a = "M";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.'))
      a = "N";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.'))
      a = "I";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-'))
      a = "A";
      if ((s.charAt(0) == ' ') && (s.charAt(1) == ' '))
      a = " ";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-') && (s.charAt(2) == '.'))
      a = "R";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.') && (s.charAt(2) == '.'))
      a = "S";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.') && (s.charAt(2) == '-'))
      a = "U";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '.'))
      a = "D"; 
      if ((s.charAt(0) == '-') && (s.charAt(1) == '-') && (s.charAt(2) == '.'))
      a = "G";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '-'))
      a = "K";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '-'))
      a = "O";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-') && (s.charAt(2) == '-'))
      a = "W";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '.') && (s.charAt(3) == '.'))
      a = "B";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '-') && (s.charAt(3) == '.'))
      a = "C";    
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.') && (s.charAt(2) == '-') && (s.charAt(3) == '.'))
      a = "F";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.') && (s.charAt(2) == '.') && (s.charAt(3) == '.'))
      a = "H";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-') && (s.charAt(2) == '-') && (s.charAt(3) == '-'))
      a = "J";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-') && (s.charAt(2) == '.') && (s.charAt(3) == '.'))
      a = "L";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '-') && (s.charAt(2) == '-') && (s.charAt(3) == '.'))
      a = "P";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '-') && (s.charAt(2) == '.') && (s.charAt(3) == '-'))
      a = "Q";
      if ((s.charAt(0) == '.') && (s.charAt(1) == '.') && (s.charAt(2) == '.') && (s.charAt(3) == '-'))
      a = "V";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '.') && (s.charAt(3) == '-'))
      a = "X";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '.') && (s.charAt(2) == '-') && (s.charAt(3) == '-'))
      a = "Y";
      if ((s.charAt(0) == '-') && (s.charAt(1) == '-') && (s.charAt(2) == '.') && (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 <  code.length(); i++){
      if (code.charAt(i) == ' '){
        for (int j = i - 4; j < i; j++){        
        character += code.charAt(j); 
        decodedCharacter = "" + decodeLetter(character);
      }
      decodedCode += decodedCharacter;
     }

    }
    System.out.println(decodedCode);    
  }
}
M.Lasko
  • 11
  • 4
  • 8
    When asking about any error or exception, always post the complete error/exception message in its entirety and indicate which line causes it. – Hovercraft Full Of Eels Oct 04 '15 at 00:54
  • What is decodeLetter? Would you mind putting the rest of the code containnig this method? – Yassin Hajaj Oct 04 '15 at 00:56
  • To update your question and provide more informations like full stacktrace, input and expected output use [edit] option placed under your post. – Pshemo Oct 04 '15 at 01:29
  • I updated the code and added the whole thing, I took out the x variable because I realized it didn't really serve any purpose because I could simply replace it with i-?, I think the problem is with the i-4, i've tried a few different values for it but it always gives me the error. – M.Lasko Oct 04 '15 at 01:43
  • What is your input value? – sstan Oct 04 '15 at 01:44
  • coded messages such as: - .... .. ... .. ... .- ... . -.-. .-. . - -- . ... ... .- --. . – M.Lasko Oct 04 '15 at 01:58
  • Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – sstan Oct 04 '15 at 02:17

2 Answers2

0
for (int j = i - 4; j < i; j++)

The above line is causing your error. It's because i is less than 4 when it reaches that line. Try the following instead:

public static void main(String[] args) {
    System.out.println("Please enter the sentence in Morse code");
    String code[] = new Scanner(System.in).nextLine().split(" ");
    String decodedCode = "";
    for(String character : code){
        decodedCode += decodeLetter(character);
    }

    System.out.println(decodedCode);
}

It splits the input into a string array by "character" and then iterates over it.

hermitmaster
  • 155
  • 1
  • 10
0

For your input, the exception happens at if ((s.charAt(0) == '.') && (s.charAt(1) == '.')) where s is just . and you are trying to access second character of it.

You should have a change in your loop to read the characters:

        for (i = 0; i < code.length(); i++) {
            if (code.charAt(i) == ' ') {
                character = "";   //Clear the value before read
                for (int j = i - 4; j < i; j++) {
                    character += code.charAt(j);
                }
                decodedCharacter = decodeLetter(character); //This should be outside the for(int j = 1-4 loop for you to read the 4 chars and then pass to decode.
                decodedCode += decodedCharacter;
            }
        }
James Jithin
  • 10,183
  • 5
  • 36
  • 51