CapitanMexico

TelephoneTranslator.java

Nov 3rd, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. /**
  4.  * Name:        CMX
  5.  * Date:        11/3/2019
  6.  * Professor:   F.
  7.  *
  8.  * Program Purpose: To translate alphabetic telephone numbers to all numbers.
  9.  */
  10.  
  11.  public class TelephoneTranslator
  12.  {
  13.      public static void main(String[] args)
  14.      {
  15.          String input;
  16.  
  17.          /**
  18.           * These arrays are matched, with any alphabet[i] corresponding with
  19.             numbers[i]. This way, we can use the same index for the replacement
  20.             function, simplifying the method greatly. This is also much more maintainable and extendible.
  21.           */
  22.          String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  23.  
  24.          String[] numbers = {"2","2","2","3","3","3","4","4","4","5","5","5","6","6","6","7","7","7","7","8","8","8","9","9","9","9"};
  25.  
  26.          StringBuilder finalNumber = new StringBuilder();
  27.  
  28.          input = JOptionPane.showInputDialog( "Input your alphabetic phone" +
  29.                             "number.\nUse only hypens (-) to seperate sets!" +
  30.                             "\nUse pattern XXX-XXX-XXXX");
  31.  
  32.         // This method helps avoid any issue with cases not matching.
  33.          input = input.toUpperCase();
  34.  
  35.          // We can now put this input through our conversion method and get the
  36.          // final converted number
  37.          finalNumber.append(convertAlphaNumeral(input, alphabet, numbers));
  38.  
  39.          // We now give this number back to the user, converted.
  40.          JOptionPane.showMessageDialog(null, "Your converted phone number is " +
  41.                                                                 finalNumber);
  42.        
  43.      }
  44.  
  45.      // This is the method we'll use to convert all the letters in the phone #
  46.      // to digits. It ignores any numbers existing and
  47.      public static String convertAlphaNumeral(String str, String[] arrayL, String[] arrayN)
  48.      {
  49.          // This replaces each letter and number pair with each iteration
  50.         for (int index = 0; index < arrayL.length && index < arrayN.length; index++)
  51.         {
  52.             String strTempL = arrayL[index];
  53.             String strTempN = arrayN[index];
  54.             str = str.replace(strTempL, strTempN);
  55.         }
  56.  
  57.         // We now return the completely converted string back to main.
  58.         return str;
  59.      }
  60.  }
Advertisement
Add Comment
Please, Sign In to add comment