JAVA - 字符串索引超出范围,我无法弄清楚原因

时间:2015-10-04 00:51:36

标签: java indexoutofrangeexception

所以我正在编写一个解码编码消息的程序,它编译但是当我运行它时,我得到一个java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1错误,我无法弄清楚这是什么来自。

以下是代码:

    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);    
  }
}

2 个答案:

答案 0 :(得分:0)

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

以上行导致您的错误。这是因为 i 在到达该行时小于4。请尝试以下方法:

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);
}

它通过“character”将输入拆分为字符串数组,然后迭代它。

答案 1 :(得分:0)

对于您的输入,异常发生在if ((s.charAt(0) == '.') && (s.charAt(1) == '.')) s只是.并且您正在尝试访问它的第二个字符。

您应该在循环中更改以读取字符:

        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;
            }
        }