将不胜感激寻找 StringIndexOutOfBoundsException 的帮助

Would appreciate some assistance finding StringIndexOutOfBoundsException

这个程序是一个由 3 个数字和 7 个字母组成的字符串,使用方法将其转换为等效数字 用户输入是一个字符串,它从字母变为数字,然后又变回由破折号分隔的数字串。 所以这只是学习的第 3 周,我被方法绊倒了。我收到的错误代码告诉我字符串索引超出范围。所以,我怀疑错误出在我的转换中,或者我在第 89 行附近连接字符串的地方。我花了 20 分钟与助教交流,希望得到任何指导。我仍然与该程序有更多关系,所以我省略了其中的一些内容,因为我只希望在我遇到卡住超过 45 min.MAIN 的地方时寻求帮助 min.MAIN 问题 - 错误发生在哪里以及为什么? 我试图在各处放置打印语句以查看发生了什么......但是我可能陷入循环。

import java.util.Scanner;
public class ConvPhoneNumTwo{
  public static void main(String[] args){
    String s = getInput();


    printPhone(convertToDigit(s));
  }   

 public static String getInput() { 
      Scanner scan = new Scanner(System.in);
      String s1 = "";
      boolean length = false;
      while (length==false){ 
        System.out.println(" Please enter a telephone number in this format ###-AAA-AAAA") ;
         s1 = scan.nextLine();
        if(s1.length() != 12 ){
          System.out.println( "this is an invalid choice try again Plz..."); 
           length = false;

        }
        else{ 
          length = true; 
        }
      }
      return s1; 
    }

 public static String convertToDigit(String s010){
    s010 = s010.toLowerCase();
    String s001= s010.substring(0,3);
    String s002 = s010.substring(4,6);
    String s003 = s010.substring(8,12);
    String s1 = (s001+s002+s003);
    String s2 = "";

    // Exceptions to our problem to stop invalid inputs 
    if (s1 == null) {
      System.out.print (" invalid input null thing"); 
    }
    if (s1.length() != 10){
      System.out.print (" invalid input"); 
    }

    String s6 = "";
    for (int i=0; i < s1.length(); i++) {


      if (Character.isDigit(s1.charAt(i))){
        s2 += s1.charAt(i); 
      }

      //sorting of the letter inputs 
      char ch = s1.charAt(i); 

      if (ch == 'a'||ch=='b'||ch== 'c'){
        s2 += "2"; 
      }
      if (ch == 'd'||ch=='e'||ch=='f'){
        s2 += "3"; 
      }
      if (ch == 'g'||ch=='h'||ch=='i'){
        s2 += "4"; 
      }
      if (ch == 'j'||ch=='k'||ch=='l'){
        s2 += "5"; 
      }
      if (ch == 'm'||ch=='n'||ch=='o'){
        s2 += "6"; 
      }
      if (ch == 'p'||ch=='q'||ch=='r'|| ch=='s'){
        s2 += "7"; 
      }
      if (ch == 't'||ch=='u'||ch=='v'){
        s2 += "8"; 
      }
      if (ch == 'w'||ch=='x'||ch=='y'|| ch=='z')
      {
        s2 += "9"; 
      }
      else{ 
      }

     String s3 = s2.substring(0,3);
    String s4 = s2.substring(3,6);
    String s5 = s2.substring(6);
     s6 = ( s3 +"-"+ s4 + "-"+ s5);
    }

    return s6; 
    }

    public static void printPhone(String message) { //print out whatever string it is given, basically System.out.println(), but through a method instead
    System.out.println(message);
  }
}

不确定我可以在其中添加多少详细信息。

我想我发现了你的问题。

在输入转数字的方法中,你是这样写的:

String s6 = "";
for (int i=0; i < s1.length(); i++) {

  if (Character.isDigit(s1.charAt(i))){
    s2 += s1.charAt(i); 
  }

  //sorting of the letter inputs 
  char ch = s1.charAt(i); 

  if (ch == 'a'||ch=='b'||ch== 'c'){
    s2 += "2"; 
  }
  if (ch == 'd'||ch=='e'||ch=='f'){
    s2 += "3"; 
  }
  if (ch == 'g'||ch=='h'||ch=='i'){
    s2 += "4"; 
  }
  if (ch == 'j'||ch=='k'||ch=='l'){
    s2 += "5"; 
  }
  if (ch == 'm'||ch=='n'||ch=='o'){
    s2 += "6"; 
  }
  if (ch == 'p'||ch=='q'||ch=='r'|| ch=='s'){
    s2 += "7"; 
  }
  if (ch == 't'||ch=='u'||ch=='v'){
    s2 += "8"; 
  }
  if (ch == 'w'||ch=='x'||ch=='y'|| ch=='z')
  {
    s2 += "9"; 
  }
  else{ 
  }

 String s3 = s2.substring(0,3);
 String s4 = s2.substring(3,6);
 String s5 = s2.substring(6);
 s6 = ( s3 +"-"+ s4 + "-"+ s5);
}

首先,您检查了输入字符串中是否已经有任何数字 - 如果有,那么您只需将数字添加到已处理的字符串中。然后你检查字母并相应地处理每个字母。

但是你有问题了。在完成已处理字符串 s2 的构建之前(在 for 循环内),您尝试提取已处理字符串的一部分。这应该在 for 循环之后完成。

因此,该方法应如下所示。

 public static String convertToDigit(String s010){
    s010 = s010.toLowerCase();
    String s001= s010.substring(0,3);
    String s002 = s010.substring(4,6);
    String s003 = s010.substring(8,12);
    String s1 = (s001+s002+s003);
    String s2 = "";

    // Exceptions to our problem to stop invalid inputs 
    if (s1 == null) {
      System.out.print (" invalid input null thing"); 
    }
    if (s1.length() != 10){
      System.out.print (" invalid input"); 
    }

    String s6 = "";
    for (int i=0; i < s1.length(); i++) {


      if (Character.isDigit(s1.charAt(i))){
        s2 += s1.charAt(i); 
      }

      //sorting of the letter inputs 
      char ch = s1.charAt(i); 

      if (ch == 'a'||ch=='b'||ch== 'c'){
        s2 += "2"; 
      }
      if (ch == 'd'||ch=='e'||ch=='f'){
        s2 += "3"; 
      }
      if (ch == 'g'||ch=='h'||ch=='i'){
        s2 += "4"; 
      }
      if (ch == 'j'||ch=='k'||ch=='l'){
        s2 += "5"; 
      }
      if (ch == 'm'||ch=='n'||ch=='o'){
        s2 += "6"; 
      }
      if (ch == 'p'||ch=='q'||ch=='r'|| ch=='s'){
        s2 += "7"; 
      }
      if (ch == 't'||ch=='u'||ch=='v'){
        s2 += "8"; 
      }
      if (ch == 'w'||ch=='x'||ch=='y'|| ch=='z')
      {
        s2 += "9"; 
      }
      else{ 
      }

// They should not be here
/*   String s3 = s2.substring(0,3);
    String s4 = s2.substring(3,6);
    String s5 = s2.substring(6);
     s6 = ( s3 +"-"+ s4 + "-"+ s5);*/
    } // end of for loop; completed constructing s2

// They should be here
String s3 = s2.substring(0,3);
String s4 = s2.substring(3,6);
String s5 = s2.substring(6);
s6 = ( s3 +"-"+ s4 + "-"+ s5);

return s6; 
}