-2

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lee
  • 21
  • 3
  • If your TA can't debug a StringOutofBounds then you are in deep trouble... btw: We really don't care about your dinner. – John3136 Mar 08 '17 at 02:54
  • 3
    as the length is `12` and indexes are zero based, then `String s003 = s010.substring(8,12);` will fail – Scary Wombat Mar 08 '17 at 02:54
  • @John3136 *We really don't care about your dinner.* Speak for yourself, I am really hungry – Scary Wombat Mar 08 '17 at 02:54
  • @ScaryWombat Shouldn't wombats be asleep now? Perhaps that's you're problem (unless you are a rare North American Wombat ;-). – John3136 Mar 08 '17 at 02:56

1 Answers1

3

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; 
}
Muntasir
  • 798
  • 1
  • 14
  • 24
  • If this is indeed the problem, it would have been easier to spot with correct indentation. It sounds like a likely explanation. – Ole V.V. Mar 08 '17 at 03:52
  • This is probably the biggest explanation behind the problem. Another explanation is also provided in the question's comments as well. – Muntasir Mar 08 '17 at 04:15