Guest User

Bad Code Challenge #40: T9

a guest
Jul 6th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.03 KB | None | 0 0
  1. public class main
  2. {
  3.     static java.util.ArrayList<String> resultArray = new java.util.ArrayList<String>();
  4.     static java.util.Hashtable<String, String> ValueDictionary = new java.util.Hashtable<String, String>();
  5.    
  6.     public static void populateValueDictionary()
  7.     {
  8.         ValueDictionary.put(" ", "0");
  9.         ValueDictionary.put("0", "00");
  10.         ValueDictionary.put("1", "11111");
  11.         ValueDictionary.put("2", "2222");
  12.         ValueDictionary.put("3", "3333");
  13.         ValueDictionary.put("4", "4444");
  14.         ValueDictionary.put("5", "5555");
  15.         ValueDictionary.put("6", "6666");
  16.         ValueDictionary.put("7", "77777");
  17.         ValueDictionary.put("8", "8888");
  18.         ValueDictionary.put("9", "99999");
  19.         ValueDictionary.put("a", "2");
  20.         ValueDictionary.put("b", "22");
  21.         ValueDictionary.put("c", "222");
  22.         ValueDictionary.put("d", "3");
  23.         ValueDictionary.put("e", "33");
  24.         ValueDictionary.put("f", "333");
  25.         ValueDictionary.put("g", "4");
  26.         ValueDictionary.put("h", "44");
  27.         ValueDictionary.put("i", "444");
  28.         ValueDictionary.put("j", "5");
  29.         ValueDictionary.put("k", "55");
  30.         ValueDictionary.put("l", "555");
  31.         ValueDictionary.put("m", "6");
  32.         ValueDictionary.put("n", "66");
  33.         ValueDictionary.put("o", "666");
  34.         ValueDictionary.put("p", "7");
  35.         ValueDictionary.put("q", "77");
  36.         ValueDictionary.put("r", "777");
  37.         ValueDictionary.put("s", "7777");
  38.         ValueDictionary.put("t", "8");
  39.         ValueDictionary.put("u", "88");
  40.         ValueDictionary.put("v", "888");
  41.         ValueDictionary.put("w", "9");
  42.         ValueDictionary.put("x", "99");
  43.         ValueDictionary.put("y", "999");
  44.         ValueDictionary.put("z", "9999");
  45.         ValueDictionary.put(".", "1");
  46.         ValueDictionary.put(",", "11");
  47.         ValueDictionary.put("?", "111");
  48.         ValueDictionary.put("!", "1111");
  49.         ValueDictionary.put("'", "*");
  50.         ValueDictionary.put("-", "**");
  51.         ValueDictionary.put("+", "***");
  52.         ValueDictionary.put("=", "****");
  53.     }
  54.    
  55.     public static void getKey(String character, int idx, String compare) throws ComparisonException
  56.     {
  57.         character = character.toLowerCase();
  58.         if(character.equals(compare)) throw new ComparisonException(ValueDictionary.get(compare), idx);
  59.     }
  60.    
  61.    
  62.     public static boolean isAllCapitalized(String word)
  63.     {
  64.         boolean isAllCapitalized = false;
  65.         for(int i = 0; i < word.length(); i++)
  66.         {
  67.             if(Character.isUpperCase(word.charAt(i)))
  68.             {
  69.                 isAllCapitalized = true;
  70.             }
  71.             else
  72.             {
  73.                 isAllCapitalized = false;
  74.             }
  75.         }
  76.         return isAllCapitalized;
  77.     }
  78.    
  79.     public static void main(String args[])
  80.     {
  81.         populateValueDictionary();
  82.        
  83.         java.util.Scanner sc = new java.util.Scanner(System.in);
  84.         StringBuilder sb = new StringBuilder();
  85.        
  86.         String delimiter = " ";
  87.        
  88.         System.out.println("Enter string to convert to T9: ");
  89.         String str = sc.nextLine();
  90.        
  91.         String[] st = str.split("\\W+");
  92.        
  93.         java.util.ArrayList<String> words = new java.util.ArrayList<String>(java.util.Arrays.asList(st));
  94.        
  95.        
  96.         for(String s : words)
  97.         {  
  98.             if(isAllCapitalized(s) && s.length() < 2)
  99.             {
  100.                 sb.append("#").append(delimiter);
  101.             }
  102.             if(isAllCapitalized(s) && s.length() > 2)
  103.             {
  104.                 sb.append("##").append(delimiter);
  105.             }
  106.             else if(!isAllCapitalized(s) && Character.isUpperCase(s.charAt(0)))
  107.             {
  108.                 sb.append("#").append(delimiter);
  109.             }
  110.  
  111.             for(int i = 0; i < s.length(); i++)
  112.             {
  113.                 String character = Character.toString(s.charAt(i));
  114.                
  115.                 try
  116.                 {
  117.                     getKey(character, i, " ");
  118.                 }
  119.                 catch(ComparisonException e){}         
  120.                 try
  121.                 {
  122.                     getKey(character, i, "0");
  123.                 }
  124.                 catch(ComparisonException e){}
  125.                 try
  126.                 {
  127.                     getKey(character, i, "1");
  128.                 }
  129.                 catch(ComparisonException e){}
  130.                 try
  131.                 {
  132.                     getKey(character, i, "2");
  133.                 }
  134.                 catch(ComparisonException e){}
  135.                 try
  136.                 {
  137.                     getKey(character, i, "3");
  138.                 }
  139.                 catch(ComparisonException e){}
  140.                 try
  141.                 {
  142.                     getKey(character, i, "4");
  143.                 }
  144.                 catch(ComparisonException e){}
  145.                 try
  146.                 {
  147.                     getKey(character, i, "5");
  148.                 }
  149.                 catch(ComparisonException e){}
  150.                 try
  151.                 {
  152.                     getKey(character, i, "6");
  153.                 }
  154.                 catch(ComparisonException e){}
  155.                 try
  156.                 {
  157.                     getKey(character, i, "7");
  158.                 }
  159.                 catch(ComparisonException e){}
  160.                 try
  161.                 {
  162.                     getKey(character, i, "8");
  163.                 }
  164.                 catch(ComparisonException e){}
  165.                 try
  166.                 {
  167.                     getKey(character, i, "9");
  168.                 }
  169.                 catch(ComparisonException e){}
  170.                 try
  171.                 {
  172.                     getKey(character, i, "a");
  173.                 }
  174.                 catch(ComparisonException e){}
  175.                 try
  176.                 {
  177.                     getKey(character, i, "b");
  178.                 }
  179.                 catch(ComparisonException e){}
  180.                 try
  181.                 {
  182.                     getKey(character, i, "c");
  183.                 }
  184.                 catch(ComparisonException e){}
  185.                 try
  186.                 {
  187.                     getKey(character, i, "d");
  188.                 }
  189.                 catch(ComparisonException e){}
  190.                 try
  191.                 {
  192.                     getKey(character, i, "e");
  193.                 }
  194.                 catch(ComparisonException e){}
  195.                 try
  196.                 {
  197.                     getKey(character, i, "f");
  198.                 }
  199.                 catch(ComparisonException e){}
  200.                 try
  201.                 {
  202.                     getKey(character, i, "g");
  203.                 }
  204.                 catch(ComparisonException e){}
  205.                 try
  206.                 {
  207.                     getKey(character, i, "h");
  208.                 }
  209.                 catch(ComparisonException e){}
  210.                 try
  211.                 {
  212.                     getKey(character, i, "i");
  213.                 }
  214.                 catch(ComparisonException e){}
  215.                 try
  216.                 {
  217.                     getKey(character, i, "j");
  218.                 }
  219.                 catch(ComparisonException e){}
  220.                 try
  221.                 {
  222.                     getKey(character, i, "k");
  223.                 }
  224.                 catch(ComparisonException e){}
  225.                 try
  226.                 {
  227.                     getKey(character, i, "l");
  228.                 }
  229.                 catch(ComparisonException e){}
  230.                 try
  231.                 {
  232.                     getKey(character, i, "m");
  233.                 }
  234.                 catch(ComparisonException e){}
  235.                 try
  236.                 {
  237.                     getKey(character, i, "n");
  238.                 }
  239.                 catch(ComparisonException e){}
  240.                 try
  241.                 {
  242.                     getKey(character, i, "o");
  243.                 }
  244.                 catch(ComparisonException e){}
  245.                 try
  246.                 {
  247.                     getKey(character, i, "p");
  248.                 }
  249.                 catch(ComparisonException e){}
  250.                 try
  251.                 {
  252.                     getKey(character, i, "q");
  253.                 }
  254.                 catch(ComparisonException e){}
  255.                 try
  256.                 {
  257.                     getKey(character, i, "r");
  258.                 }
  259.                 catch(ComparisonException e){}
  260.                 try
  261.                 {
  262.                     getKey(character, i, "s");
  263.                 }
  264.                 catch(ComparisonException e){}
  265.                 try
  266.                 {
  267.                     getKey(character, i, "t");
  268.                 }
  269.                 catch(ComparisonException e){}
  270.                 try
  271.                 {
  272.                     getKey(character, i, "u");
  273.                 }
  274.                 catch(ComparisonException e){}
  275.                 try
  276.                 {
  277.                     getKey(character, i, "v");
  278.                 }
  279.                 catch(ComparisonException e){}
  280.                 try
  281.                 {
  282.                     getKey(character, i, "w");
  283.                 }
  284.                 catch(ComparisonException e){}
  285.                 try
  286.                 {
  287.                     getKey(character, i, "x");
  288.                 }
  289.                 catch(ComparisonException e){}
  290.                 try
  291.                 {
  292.                     getKey(character, i, "y");
  293.                 }
  294.                 catch(ComparisonException e){}
  295.                 try
  296.                 {
  297.                     getKey(character, i, "z");
  298.                 }
  299.                 catch(ComparisonException e){}
  300.                 try
  301.                 {
  302.                     getKey(character, i, ".");
  303.                 }
  304.                 catch(ComparisonException e){}
  305.                 try
  306.                 {
  307.                     getKey(character, i, ",");
  308.                 }
  309.                 catch(ComparisonException e){}
  310.                 try
  311.                 {
  312.                     getKey(character, i, "?");
  313.                 }
  314.                 catch(ComparisonException e){}
  315.                 try
  316.                 {
  317.                     getKey(character, i, "!");
  318.                 }
  319.                 catch(ComparisonException e){}
  320.                 try
  321.                 {
  322.                     getKey(character, i, "'");
  323.                 }
  324.                 catch(ComparisonException e){}
  325.                 try
  326.                 {
  327.                     getKey(character, i, "-");
  328.                 }
  329.                 catch(ComparisonException e){}
  330.                 try
  331.                 {
  332.                     getKey(character, i, "+");
  333.                 }
  334.                 catch(ComparisonException e){}
  335.                 try
  336.                 {
  337.                     getKey(character, i, "=");
  338.                 }
  339.                 catch(ComparisonException e){}     
  340.             }
  341.            
  342.             int r = 0;
  343.             for(String t : resultArray)
  344.             {
  345.                 sb.append(t).append(delimiter);
  346.                 sb.append("_");
  347.             }
  348.            
  349.             if(words.indexOf(s) < words.size() - 1)
  350.             {
  351.                 sb.append("0").append(delimiter);
  352.             }
  353.            
  354.             if(r > 1 && words.get(r).equals(words.get(r-1)))
  355.             {
  356.                 sb.append("_");
  357.             }
  358.            
  359.             r++;
  360.            
  361.             resultArray.clear();
  362.         }
  363.         System.out.println(sb.substring(0, sb.length()-1));
  364.     }
  365.    
  366.  
  367. }
  368.  
  369.  
  370. /* ComparisonException class, which was in a different file */
  371.  
  372. public class ComparisonException extends Exception
  373. {
  374.     private static final long serialVersionUID = 1L;
  375.  
  376.     public ComparisonException(String val, int idx)
  377.     {
  378.         main.resultArray.add(val);
  379.     }
  380. }
Add Comment
Please, Sign In to add comment