Search code examples
javastringindexoutofbounds

Would appreciate some assistance finding StringIndexOutOfBoundsException


This program is the String of 3 numbers then 7 letters to be transformed into its equivalent number using methods The user input is a string which is changed from letters to digits and back into a string of numbers separated by dashes. So this is only week 3ish of learning and i got tripped up by methods. The error code I am receiving is telling me the string index is out of bounds. So, i suspect the error is in my conversion or where i concatenated the strings at the end around line 89. I spent 20 min with the TA and would appreciate any guidance. I still have more to do with the program so i am omitting some of it because I would only like help when i come to a place that I become stuck for more than 45 min.MAIN QUESTION- Where does the error occur and why? I attempted to put print statements everywhere to see what was happening... however I may be stuck in a loop.

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

Not sure about how much more detail i can put into this.


Solution

  • I think I found your problem.

    In the method for converting input to digits, you wrote this:

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

    Firstly, you have checked whether there are already any digit in the input string - if there is any, then you simply added the digit to processed string. Then you checked for letters and treated each letter accordingly.

    But then you have a problem. Before completing construction of the processed string s2 (within the for loop), you tried to extract parts of your processed string. This should be done after the for loop.

    So, the method should look something like this.

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