1 Replies - 478 Views - Last Post: 22 April 2010 - 06:56 PM Rate Topic: -----

#1 Guest_Andrew*


Reputation:

Phone Number to Letters

Posted 22 April 2010 - 06:19 PM

Okay guys. I figured out my homework of listing out all the possible combinations for the letters in a phone number. Can you guys help me clean up the code a bit?

import java.util.Scanner;

public class NumberToWord {

	public static void main(String[] args) {

		char number2[] = { 'A', 'B', 'C' };
		char number3[] = { 'D', 'E', 'F' };
		char number4[] = { 'G', 'H', 'I' };
		char number5[] = { 'J', 'K', 'L' };
		char number6[] = { 'M', 'N', 'O' };
		char number7[] = { 'P', 'R', 'S' };
		char number8[] = { 'T', 'U', 'V' };
		char number9[] = { 'W', 'X', 'Y' };

		char Letter0[] = { ' ',' ',' ' };
		char Letter1[] = { ' ',' ',' ' };
		char Letter2[] = { ' ',' ',' ' };
		char Letter3[] = { ' ',' ',' ' };
		char Letter4[] = { ' ',' ',' ' };
		char Letter5[] = { ' ',' ',' ' };
		char Letter6[] = { ' ',' ',' ' };
		char Letter7[] = { ' ',' ',' ' };

		Scanner scanner = new Scanner( System.in );
		Character phoneArray[] = new Character[ 7 ];

		System.out.print( "Enter a phone: " );
		String phoneNumber = scanner.nextLine();

		//Convert phoneNumber string to array of characters
		for( int i=0; i < phoneNumber.length(); i++ ) {
			phoneArray[i]=(new Character(phoneNumber.charAt(i)));
		}

		for (int i=0;i < phoneArray.length; i++) {
			if ( phoneArray[i].equals('0') ){
				System.out.println("Invalid Input (0)");
				System.exit(1);
			}
			else if ( phoneArray[i].equals('1') ){
				System.out.println("Invalid Input (1)");
				System.exit(1);
			}
		}

//COMPARE FIRST DIGIT
if (phoneArray[0].equals('2'))
	for (int j = 0; j < Letter0.length;j++)
		Letter0[j] = number2[j];
	else if (phoneArray[0].equals('3'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number3[j];
	else if (phoneArray[0].equals('4'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number4[j];
	else if (phoneArray[0].equals('5'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number5[j];
	else if (phoneArray[0].equals('6'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number6[j];
	else if (phoneArray[0].equals('7'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number7[j];
	else if (phoneArray[0].equals('8'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number8[j];
	else if (phoneArray[0].equals('9'))
		for (int j = 0; j < Letter0.length;j++)
			Letter0[j] = number9[j];

//COMPARE SECOND DIGIT
	if (phoneArray[1].equals('2'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number2[j];
	else if (phoneArray[1].equals('3'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number3[j];
	else if (phoneArray[1].equals('4'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number4[j];
	else if (phoneArray[1].equals('5'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number5[j];
	else if (phoneArray[1].equals('6'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number6[j];
	else if (phoneArray[1].equals('7'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number7[j];
	else if (phoneArray[1].equals('8'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number8[j];
	else if (phoneArray[1].equals('9'))
		for (int j = 0; j < Letter1.length;j++)
			Letter1[j] = number9[j];


	if (phoneArray[2].equals('2'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number2[j];
	else if (phoneArray[2].equals('3'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number3[j];
	else if (phoneArray[2].equals('4'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number4[j];
	else if (phoneArray[2].equals('5'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number5[j];
	else if (phoneArray[2].equals('6'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number6[j];
	else if (phoneArray[2].equals('7'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number7[j];
	else if (phoneArray[2].equals('8'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number8[j];
	else if (phoneArray[2].equals('9'))
		for (int j = 0; j < Letter2.length;j++)
			Letter2[j] = number9[j];


	if (phoneArray[3].equals('2'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number2[j];
	else if (phoneArray[3].equals('3'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number3[j];
	else if (phoneArray[3].equals('4'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number4[j];
	else if (phoneArray[3].equals('5'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number5[j];
	else if (phoneArray[3].equals('6'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number6[j];
	else if (phoneArray[3].equals('7'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number7[j];
	else if (phoneArray[3].equals('8'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number8[j];
	else if (phoneArray[3].equals('9'))
		for (int j = 0; j < Letter3.length;j++)
			Letter3[j] = number9[j];

	if (phoneArray[4].equals('2'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number2[j];
	else if (phoneArray[4].equals('3'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number3[j];
	else if (phoneArray[4].equals('4'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number4[j];
	else if (phoneArray[4].equals('5'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number5[j];
	else if (phoneArray[4].equals('6'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number6[j];
	else if (phoneArray[4].equals('7'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number7[j];
	else if (phoneArray[4].equals('8'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number8[j];
	else if (phoneArray[4].equals('9'))
		for (int j = 0; j < Letter4.length;j++)
			Letter4[j] = number9[j];


	if (phoneArray[5].equals('2'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number2[j];
	else if (phoneArray[5].equals('3'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number3[j];
	else if (phoneArray[5].equals('4'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number4[j];
	else if (phoneArray[5].equals('5'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number5[j];
	else if (phoneArray[5].equals('6'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number6[j];
	else if (phoneArray[5].equals('7'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number7[j];
	else if (phoneArray[5].equals('8'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number8[j];
	else if (phoneArray[5].equals('9'))
		for (int j = 0; j < Letter5.length;j++)
			Letter5[j] = number9[j];

	if (phoneArray[6].equals('2'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number2[j];
	else if (phoneArray[6].equals('3'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number3[j];
	else if (phoneArray[6].equals('4'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number4[j];
	else if (phoneArray[6].equals('5'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number5[j];
	else if (phoneArray[6].equals('6'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number6[j];
	else if (phoneArray[6].equals('7'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number7[j];
	else if (phoneArray[6].equals('8'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number8[j];
	else if (phoneArray[6].equals('9'))
		for (int j = 0; j < Letter6.length;j++)
			Letter6[j] = number9[j];

//
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			for (int k = 0; k < 3; k++) {
				for (int l = 0; l < 3; l++) {
					for (int m = 0; m < 3; m++) {
						for (int n = 0; n < 3; n++) {
							for (int o = 0; o<3; o++) {
								System.out.print(Letter0[i]);
								System.out.print(" ");
								System.out.print(Letter1[j]);
								System.out.print(" ");
								System.out.print(Letter2[k]);
								System.out.print(" ");
								System.out.print(Letter3[l]);
								System.out.print(" ");
								System.out.print(Letter4[m]);
								System.out.print(" ");
								System.out.print(Letter5[n]);
								System.out.print(" ");
								System.out.print(Letter6[o]);
								System.out.println();
								}
							}
						}
					}
				} //end k for
			} //end j for
		} // end i for
	}
}


Thanks!

Is This A Good Question/Topic? 0

Replies To: Phone Number to Letters

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Phone Number to Letters

Posted 22 April 2010 - 06:56 PM

Your doing quite a bit there, in what way did you want to clean it up? Looks ok to me, and if it works correctly, shouldnt be a problem. If you wanted to make a change, you could do this whole program using a 2d Array, but that would be a lot of changes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1