Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    telephone word generator

    Hi everybody, very new to this forum and I very much like what I see. I have a coding assignment due very soon and it is very very rough. It is basically a telephone word generator, where I ask the user to enter a seven digit telephone number (sans 0 and 1) and then the program generates all possible letter combinations. I then have to use a printstream object to write to a file all the possible word combinations. It works, but can you help me clean it up. This is what I have so far:


    Code:
    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( "Please 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];
    
    //COMPARE THIRD DIGIT
    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];
    
    //COMPARE FORTH DIGIT
    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];
    
    //COMPARE FIFTH DIGIT
    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];
    
    //COMPARE SIXTH DIGIT
    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];
    
    //COMPARE SEVENTH DIGIT
    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];
    
    //PRINT
    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
    }
    }
    Last edited by WA; 01-31-2009 at 08:44 PM.

  • #2
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Please be more specific what you mean by "clean it up". Also, try and format any code using the CODE tag in your post- it makes it a lot easier to read.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •