Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Easy18 {
- // Goal: convert a 1-800 number given in letters to numbers.
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- char[] input;
- // get input
- System.out.print("Enter the last seven letters (upper or lower case) and/or numbers of a phone number.\nInput: 1-800-");
- boolean good = true; // whether or not the input is acceptable
- // this do-while loop only exits when input has length of 7.
- do {
- if (!good) System.out.print("Please enter seven letters and/or numbers.\nInput: 1-800-");
- input = s.nextLine().toLowerCase().toCharArray();
- good = true;
- if (input.length != 7) good = false;
- } while (!good);
- // generate output
- int[] cap = {0,0,3,6,9,12,15,19,22,26};
- // cap[] helps identify how large each set of chars is
- // e.g. "abc" has 3 (at index 2) and "pqrs" has 4 (at index 7)
- System.out.print("Output: 1-800-");
- for (int i=0;i<7;i++) {
- if (i==3) System.out.print("-"); // print the -
- // case 1: the input is a number. simply print the number.
- if (input[i] >=48 && input[i] <= 57) System.out.print(input[i]);
- // case 2: the input is a char. note the keycode of 'a' is 97.
- else
- for (int j=2;j<cap.length;j++) {
- if (input[i] < 97+cap[j]) {
- System.out.print(j);
- break;
- }
- }
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement