Advertisement
patrickgh3

Untitled

Mar 9th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Easy18 {
  4.     // Goal: convert a 1-800 number given in letters to numbers.
  5.     public static void main(String[] args) {
  6.         Scanner s = new Scanner(System.in);
  7.         char[] input;
  8.        
  9.         // get input
  10.         System.out.print("Enter the last seven letters (upper or lower case) and/or numbers of a phone number.\nInput: 1-800-");
  11.         boolean good = true; // whether or not the input is acceptable
  12.         // this do-while loop only exits when input has length of 7.
  13.         do {
  14.             if (!good) System.out.print("Please enter seven letters and/or numbers.\nInput: 1-800-");
  15.             input = s.nextLine().toLowerCase().toCharArray();
  16.             good = true;
  17.             if (input.length != 7) good = false;
  18.         } while (!good);
  19.        
  20.         // generate output
  21.         int[] cap = {0,0,3,6,9,12,15,19,22,26};
  22.         // cap[] helps identify how large each set of chars is
  23.         // e.g. "abc" has 3 (at index 2) and "pqrs" has 4 (at index 7)
  24.         System.out.print("Output: 1-800-");
  25.         for (int i=0;i<7;i++) {
  26.             if (i==3) System.out.print("-"); // print the -
  27.             // case 1: the input is a number. simply print the number.
  28.             if (input[i] >=48 && input[i] <= 57) System.out.print(input[i]);
  29.             // case 2: the input is a char. note the keycode of 'a' is 97.
  30.             else
  31.             for (int j=2;j<cap.length;j++) {
  32.                 if (input[i] < 97+cap[j]) {
  33.                     System.out.print(j);
  34.                     break;
  35.                 }
  36.             }
  37.         }
  38.         System.out.println();
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement