Advertisement
Sinux1

phonepad

May 4th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class Q3 {
  5.     private static Scanner kb;
  6.     private static String input;
  7.     private static HashMap<Character, String> maps;
  8.     private static int counter = 0;
  9.  
  10.     public static void printCombos()
  11.     {
  12.         printCombosRecurse("", 0);
  13.     }
  14.  
  15.     public static void step(String pass, int x, int length, int index)
  16.     {
  17.         if(x < length)
  18.         {
  19.             printCombosRecurse(pass + maps.get(input.charAt(index)).charAt(x), index + 1 );
  20.             step(pass, x+1, length, index);
  21.         }
  22.     }
  23.     public static void printCombosRecurse(String pass, int index)
  24.     {
  25.         if(pass.length() == input.length())
  26.             {
  27.                 System.out.println(pass);
  28.                 counter++;
  29.             }
  30.         else
  31.         {
  32.             step(pass, 0, maps.get(input.charAt(index)).length(), index);
  33.  
  34.         }
  35.     }
  36.  
  37.     public static void main(String[] args)
  38.     {
  39.     maps = new HashMap<Character, String>();
  40.     kb = new Scanner(System.in);
  41.     maps.put('0', "0");
  42.     maps.put('1', "1");
  43.     maps.put('2', "ABC");
  44.     maps.put('3', "DEF");
  45.     maps.put('4', "GHI");
  46.     maps.put('5', "JKL");
  47.     maps.put('6', "MNO");
  48.     maps.put('7', "PQRS");
  49.     maps.put('8', "TUV");
  50.     maps.put('9', "WXYZ");
  51.  
  52.     do
  53.     {
  54.         System.out.println("Enter a phone number (numbers only). Type \"exit\" to end.");
  55.         input = kb.nextLine();
  56.         if(!input.equals("exit"))
  57.         {
  58.             printCombos();
  59.             System.out.println(counter + " option(s) are available");
  60.             counter = 0;
  61.         }
  62.  
  63.     }while(!input.equals("exit"));
  64.     System.out.println("Bye!");
  65.  
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement