1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java-converting phone numbers into words

  1. Nov 30, 2011 #1
    Java--converting phone numbers into words

    1. The problem statement, all variables and given/known data

    I'm making a program that asks the user for a file with a bunch of phone numbers in the form:
    XXX-XXXX
    XXX-XXXX
    XXX-XXXX
    where X is an integer
    etc.

    The program takes that file, and compares it to a file with a bunch of words. This is our wordbank. It is in the form:

    ABCDEFG
    IEJFIGO
    LAIEFIG
    etc.

    I've written my code completely, and it compiles, but when I run it, after askign the user for the files, it just...doesn't do anything...or it keeps doing something and won't actually do what it's supposed to do.

    3. The attempt at a solution

    This is my code. yes it is messy, yes I will use methods later, but I just wanted to get the code working first. It seems that it should work, but it's not for some reason. Thank you all in advance.








    import java.util.*;
    import java.io.*;

    public class PhoneTranslator
    {
    public static void main(String[] args) throws Exception
    {
    Scanner in = new Scanner(System.in);
    //Asks user what to do
    System.out.println("This program reads phone numbers from a text file and determines if these numbers can be made into meaningful, English words. ");
    System.out.println("You may specify your own file if you wish, but you can and should use the given files already. Output should be 'output.txt', words are from 'words.txt', and ");
    System.out.println("phone numbers are from 'phones.txt'.");
    System.out.print("Enter the directory and filename of the message you wish to read phone numbers from, without '.txt': ");
    String filename = in.nextLine();
    //Creates a file instance
    java.io.File file = new java.io.File(filename + ".txt");
    //Error if the file doesn't exist
    if (file.exists() == false)
    {
    System.out.println("The file specified does not exist.");
    System.exit(0);
    }

    System.out.print("Enter the directory and filename of the file with the word bank you wish to use, without '.txt': ");
    String wordsFilename = in.nextLine();
    //Creates a file instance for the phone numbers
    java.io.File wordfile = new java.io.File(wordsFilename + ".txt");
    //Error if the phone number file doesn't exist
    if (wordfile.exists() == false)
    {
    System.out.println("The file specified does not exist.");
    System.exit(0);
    }

    //Creates an output file
    System.out.print("Enter the directory and filename that you want phone numbers and words stored in, without '.txt': ");
    String newFileName = in.nextLine();
    java.io.PrintWriter newFile = new java.io.PrintWriter(newFileName + ".txt");
    java.io.PrintWriter output = new java.io.PrintWriter(newFile);

    Scanner input = new Scanner(file);
    //Sets phoneNumbers to the empty string
    String phoneNumbers = "";
    //Program uses a delimiter that would most likely not be in the message that is to be converted.
    input.useDelimiter(" ");

    //Reads phone numbers from the file
    while (input.hasNext())
    {
    phoneNumbers = input.next();
    }
    input.close();


    Scanner scan = new Scanner(wordfile);
    String phoneWords = "";
    scan.useDelimiter(" ");
    //Reads words from the wordbank
    while (scan.hasNext())
    {
    phoneWords = in.next();
    }
    scan.close();

    //Takes each phone number, turn it into a string
    StringTokenizer token = new StringTokenizer(phoneNumbers);
    int count = 0;
    //How to get rid of 99?????
    String[] phoneNumberList = new String[99];
    while (token.hasMoreTokens())
    {
    phoneNumberList[count] = token.nextToken();
    System.out.println(phoneNumberList[count]);
    count++;
    }

    //Takes each word, turns each one into a string
    StringTokenizer wordString = new StringTokenizer(phoneWords);
    int count2 = 0;
    //How to get rid of 99?????
    String[] phoneWordList = new String[99];
    while (wordString.hasMoreTokens())
    {
    phoneWordList[count2] = token.nextToken();
    System.out.println(phoneWordList[count2]);
    count2++;
    }

    //Convert each phone number string into an actual integer phone number. In other words, take the dash out!
    int[] phoneNumberListInt = new int[99];
    for(int ind = 0; ind < 99; ind ++)
    {
    phoneNumberList[ind] = phoneNumberList[ind].substring(0, 3) + phoneNumberList[ind].substring(3);
    phoneNumberListInt[ind] = Integer.parseInt(phoneNumberList[ind]);
    }

    //Convert each individual word to a string of what it's number would be on a touchtone phone
    String[] wordListConvertedToNumbers = new String[99];
    for (int ind = 0; ind < 99; ind ++)
    {
    int count3 = 0;
    while (count3 < 7)
    {
    if (phoneWordList[ind].charAt(count3) == 'A' || phoneWordList[ind].charAt(count3) == 'a' ||
    phoneWordList[ind].charAt(count3) == 'B' || phoneWordList[ind].charAt(count3) == 'b' ||
    phoneWordList[ind].charAt(count3) == 'C' || phoneWordList[ind].charAt(count3) == 'c')
    wordListConvertedToNumbers[ind] += '2';
    else if(phoneWordList[ind].charAt(count3) == 'D' || phoneWordList[ind].charAt(count3) == 'd' ||
    phoneWordList[ind].charAt(count3) == 'E' || phoneWordList[ind].charAt(count3) == 'e' ||
    phoneWordList[ind].charAt(count3) == 'F' || phoneWordList[ind].charAt(count3) == 'f')
    wordListConvertedToNumbers[ind] += '3';
    else if(phoneWordList[ind].charAt(count3) == 'G' || phoneWordList[ind].charAt(count3) == 'g' ||
    phoneWordList[ind].charAt(count3) == 'H' || phoneWordList[ind].charAt(count3) == 'h' ||
    phoneWordList[ind].charAt(count3) == 'I' || phoneWordList[ind].charAt(count3) == 'i')
    wordListConvertedToNumbers[ind] += '4';
    else if(phoneWordList[ind].charAt(count3) == 'J' || phoneWordList[ind].charAt(count3) == 'j' ||
    phoneWordList[ind].charAt(count3) == 'K' || phoneWordList[ind].charAt(count3) == 'k' ||
    phoneWordList[ind].charAt(count3) == 'L' || phoneWordList[ind].charAt(count3) == 'l')
    wordListConvertedToNumbers[ind] += '5';
    else if(phoneWordList[ind].charAt(count3) == 'M' || phoneWordList[ind].charAt(count3) == 'm' ||
    phoneWordList[ind].charAt(count3) == 'N' || phoneWordList[ind].charAt(count3) == 'n' ||
    phoneWordList[ind].charAt(count3) == 'O' || phoneWordList[ind].charAt(count3) == 'o')
    wordListConvertedToNumbers[ind] += '6';
    else if(phoneWordList[ind].charAt(count3) == 'P' || phoneWordList[ind].charAt(count3) == 'p' ||
    phoneWordList[ind].charAt(count3) == 'R' || phoneWordList[ind].charAt(count3) == 'r' ||
    phoneWordList[ind].charAt(count3) == 'S' || phoneWordList[ind].charAt(count3) == 's')
    wordListConvertedToNumbers[ind] += '7';
    else if(phoneWordList[ind].charAt(count3) == 'T' || phoneWordList[ind].charAt(count3) == 't' ||
    phoneWordList[ind].charAt(count3) == 'U' || phoneWordList[ind].charAt(count3) == 'u' ||
    phoneWordList[ind].charAt(count3) == 'V' || phoneWordList[ind].charAt(count3) == 'v')
    wordListConvertedToNumbers[ind] += '8';
    else if(phoneWordList[ind].charAt(count3) == 'W' || phoneWordList[ind].charAt(count3) == 'w' ||
    phoneWordList[ind].charAt(count3) == 'X' || phoneWordList[ind].charAt(count3) == 'x' ||
    phoneWordList[ind].charAt(count3) == 'Y' || phoneWordList[ind].charAt(count3) == 'y')
    wordListConvertedToNumbers[ind] += '9';

    count++;
    }
    }

    //Convert each individual words' numbers from a string to an integer
    int[] wordListConvertedToInteger = new int[99];
    for(int ind = 0; ind < 99; ind ++)
    {
    wordListConvertedToInteger[ind] = Integer.parseInt(wordListConvertedToNumbers[ind]);
    }

    //Compare our phone numbers (our integers) to the 7 digit numbers that correspond to the words from our word bank
    for (int ind = 0; ind < 99; ind ++)
    {
    int count4 = 0;
    while (count4 < 99)
    {
    if (phoneNumberListInt[ind] == wordListConvertedToInteger[count4])
    System.out.println(phoneNumberList[ind] + " " + phoneWordList[count4]);
    else
    System.out.println(phoneNumberList[ind] + " NO WORDS");
    count4++;
    }
    }


    }
    }
     
  2. jcsd
  3. Dec 1, 2011 #2

    Mark44

    Staff: Mentor

    Re: Java--converting phone numbers into words

    Two comments:
    1) When you post code, put it between [noparse]
    Code (Text):
     and
    [/noparse] tags. Code that is formatted badly is difficult to read.
    2) When your code runs, but it doesn't seem to be doing anything, a debugger is a great help.
    At the very least, you can fall back to the old standby - print statements embedded all over the place so you can see where your program is hanging up. The print statements should convey useful information about where in the program they are and the values of important variables.
     
  4. Dec 1, 2011 #3
    Re: Java--converting phone numbers into words

    I'm not familiar with a debugger nor am I familiar with print statements...

    Sorry i will use the
    Code (Text):
     from now on.  But I have since determined that my problem might lie here:

    [code]
    //Convert each phone number string into an actual integer phone number. In other words, take the dash out!
            int[] phoneNumberListInt = new int[99];
            for(int ind = 0; ind < 99; ind ++)
            {
                phoneNumberList[ind] = phoneNumberList[ind].substring(0, 3) + phoneNumberList[ind].substring(4);
                phoneNumberListInt[ind] = Integer.parseInt(phoneNumberList[ind]);
            }
     
    I need to get rid of the dashes, and I've done so by using that code. Then I make the string of numbers into an actual integer. numbers are in the form XXX-XXXX, I want to remove the dash and have a string "XXXXXXX" and then convert that string to an integer XXXXXXX.
     
  5. Dec 1, 2011 #4
    Re: Java--converting phone numbers into words

    Okay I have given up trying to convert it to an integer. Let's just take the string XXX-XXXX and take away the minus sign by concatinating two substrings from 0 to 3, then 4 to the end.

    Then we'll use if( phonenumber1.equals(phonenumberonewithnodash)) or whatever. I try that and i still get:

    Code (Text):

    Exception in thread "main" java.lang.NullPointerException
        at helloworld.main(helloworld.java:87)
     
    Here is line 87:

    Code (Text):

    //Convert each phone number string into an actual integer phone number. In other words, take the dash out!
            String[] newphoneNumberList = new String[99];
            for(int ind = 0; ind < 99; ind ++)
            {
                newphoneNumberList[ind] = phoneNumberList[ind].substring(0, 3) + phoneNumberList[ind].substring(3); <<THIS IS LINE 87
                //phoneNumberListInt[ind] = Integer.parseInt(phoneNumberList[ind]);
            }
     
    I think what the issue might be is that I've created an array of 99 variables throughout my code. There are not 99 phone numbers, there are like 20 or something like that. I chose 99 to be safe, in case the user decides to choose a file with 40 phone numbers. If this is the problem, then how do I choose my array sizes to make them the exact same size as how many phone numbers there are? Recall that we did turn each phone number into a bunch of strings.
     
  6. Dec 1, 2011 #5
    Re: Java--converting phone numbers into words

    Got it working, my issue was with the sizes of the arrays. Here is my code so far:

    Code (Text):

    /**
     *Program Name: MessageEncryption
     *Author:           XXXXXXXXXXX
     *Date:              Nov 30, 2011
     *Course:            XXXXXXXXXX
     *Instructor:      XXXXXXXXXXXXX
     *Compiler:         JDK 1.5
      */
     
    /**
     
     */

    import java.util.*;
    import java.io.*;

    public class PhoneTranslator
    {
        public static void main(String[] args) throws Exception
        {
            Scanner in = new Scanner(System.in);
            //Asks user what to do
            System.out.println("This program reads phone numbers from a text file and determines if these numbers can be made into meaningful, English words.");
            System.out.println("Phone numbers will be read from 'phones.txt', words will be chosen from 'words.txt', and the program will display the output" +
            " on the console, and store it in 'output.txt'");
            //Creates a file instance
            java.io.File file = new java.io.File("phones.txt");
           

            java.io.File wordfile = new java.io.File("words.txt");

           
            //Creates an output file
            System.out.print("Press enter to begin the process.");
            String newFileName = in.nextLine();
            java.io.PrintWriter newFile = new java.io.PrintWriter("output.txt");
            java.io.PrintWriter output = new java.io.PrintWriter(newFile);
           
            Scanner input = new Scanner(file);
            //Sets phoneNumbers to the empty string
            String phoneNumbers = "";
            //Program uses a delimiter that would most likely not be in the message that is to be converted.
            input.useDelimiter("                      ");
           
            //Reads phone numbers from the file
            while (input.hasNext())
            {
                phoneNumbers = input.next();
            }
            input.close();
           
           
            Scanner scan = new Scanner(wordfile);
            String phoneWords = "";
            scan.useDelimiter("             ");
           
            //Reads words from the wordbank
            while (scan.hasNext())
            {
                phoneWords = scan.next();
            }
            scan.close();

            //Takes each phone number, turn it into a string
            StringTokenizer token = new StringTokenizer(phoneNumbers);
            int count = 0;
            String[] phoneNumberList = new String[29];
            while (token.hasMoreTokens())
            {
                    phoneNumberList[count] = token.nextToken();
                    count++;
            }
           
            //Takes each word, turns each one into a string
            StringTokenizer token2 = new StringTokenizer(phoneWords);
            int count2 = 0;
            String[] phoneWordList = new String[33];
            while (token2.hasMoreTokens())
            {
                    phoneWordList[count2] = token2.nextToken();
                    count2++;
            }
           
            //Remove the dash from each phone number.
            String[] newphoneNumberList = new String[29];
            for(int ind = 0; ind < 29; ind ++)
            {
                newphoneNumberList[ind] = phoneNumberList[ind].substring(0, 3) + phoneNumberList[ind].substring(4);
            }      
           
            //Convert each individual word to a string of what it's number would be on a touchtone phone
            String[] wordListConvertedToNumbers = new String[33];
            for(int ind = 0; ind < 33; ind ++)
                wordListConvertedToNumbers[ind] = "";
            for (int ind = 0; ind < 33; ind ++)
            {
                int count3 = 0;
                while (count3 < 7)
                {
                    if (phoneWordList[ind].charAt(count3) == 'A' || phoneWordList[ind].charAt(count3) == 'a' ||
                        phoneWordList[ind].charAt(count3) == 'B' || phoneWordList[ind].charAt(count3) == 'b' ||
                    phoneWordList[ind].charAt(count3) == 'C' || phoneWordList[ind].charAt(count3) == 'c')
                        wordListConvertedToNumbers[ind] += '2';
                    else if(phoneWordList[ind].charAt(count3) == 'D' || phoneWordList[ind].charAt(count3) == 'd' ||
                        phoneWordList[ind].charAt(count3) == 'E' || phoneWordList[ind].charAt(count3) == 'e' ||
                    phoneWordList[ind].charAt(count3) == 'F' || phoneWordList[ind].charAt(count3) == 'f')
                        wordListConvertedToNumbers[ind] += '3';
                    else if(phoneWordList[ind].charAt(count3) == 'G' || phoneWordList[ind].charAt(count3) == 'g' ||
                        phoneWordList[ind].charAt(count3) == 'H' || phoneWordList[ind].charAt(count3) == 'h' ||
                    phoneWordList[ind].charAt(count3) == 'I' || phoneWordList[ind].charAt(count3) == 'i')
                        wordListConvertedToNumbers[ind] += '4';
                    else if(phoneWordList[ind].charAt(count3) == 'J' || phoneWordList[ind].charAt(count3) == 'j' ||
                        phoneWordList[ind].charAt(count3) == 'K' || phoneWordList[ind].charAt(count3) == 'k' ||
                    phoneWordList[ind].charAt(count3) == 'L' || phoneWordList[ind].charAt(count3) == 'l')
                        wordListConvertedToNumbers[ind] += '5';
                    else if(phoneWordList[ind].charAt(count3) == 'M' || phoneWordList[ind].charAt(count3) == 'm' ||
                        phoneWordList[ind].charAt(count3) == 'N' || phoneWordList[ind].charAt(count3) == 'n' ||
                    phoneWordList[ind].charAt(count3) == 'O' || phoneWordList[ind].charAt(count3) == 'o')
                        wordListConvertedToNumbers[ind] += '6';
                    else if(phoneWordList[ind].charAt(count3) == 'P' || phoneWordList[ind].charAt(count3) == 'p' ||
                        phoneWordList[ind].charAt(count3) == 'R' || phoneWordList[ind].charAt(count3) == 'r' ||
                    phoneWordList[ind].charAt(count3) == 'S' || phoneWordList[ind].charAt(count3) == 's')
                        wordListConvertedToNumbers[ind] += '7';
                    else if(phoneWordList[ind].charAt(count3) == 'T' || phoneWordList[ind].charAt(count3) == 't' ||
                        phoneWordList[ind].charAt(count3) == 'U' || phoneWordList[ind].charAt(count3) == 'u' ||
                    phoneWordList[ind].charAt(count3) == 'V' || phoneWordList[ind].charAt(count3) == 'v')
                        wordListConvertedToNumbers[ind] += '8';
                    else if(phoneWordList[ind].charAt(count3) == 'W' || phoneWordList[ind].charAt(count3) == 'w' ||
                        phoneWordList[ind].charAt(count3) == 'X' || phoneWordList[ind].charAt(count3) == 'x' ||
                    phoneWordList[ind].charAt(count3) == 'Y' || phoneWordList[ind].charAt(count3) == 'y')
                        wordListConvertedToNumbers[ind] += '9';
                   
                    count3++;
                }
            }
           
            //Finally, display the output.
            for (int ind = 0; ind < 29; ind ++)
            {
                System.out.print(phoneNumberList[ind] + "     ");
                int count4 = 0;
                while (count4 < 33)
                {
                    if (((newphoneNumberList[ind].equals(wordListConvertedToNumbers[count4])) == false) && count4 < 32)
                    {
                        count4++;
                    }
                    else if (((newphoneNumberList[ind].equals(wordListConvertedToNumbers[count4])) == false) && count4 == 32)
                    {
                        System.out.print("NO WORDS");
                        count4++;
                    }
                    else
                    {
                        while (count4 < 33)
                        {
                            if (newphoneNumberList[ind].equals(wordListConvertedToNumbers[count4]))
                            {
                                System.out.print(phoneWordList[count4] + " ");
                                count4++;
                            }
                            else if ((newphoneNumberList[ind].equals(wordListConvertedToNumbers[count4])) == false)
                            {
                                count4++;
                            }

                        }
                    }
                }
                System.out.println();
               
            }
        }
    }
     
    Now since instructor's stress top-down design so much, the only thing left to do is take each problem (see code comments) and turn it into a method, based on the code I have put in, and then write the output to a new text file. Shouldn't be too hard.

    My only question is this: is there any way of changing all my array sizes so that they are ambiguous? The numbers you see are based on the two files we were given, 'phones.txt' and 'words.txt', but let's say someone wanted to use the program on another file with the same name, but with a different number of words or phone numbers? What if there are 10000 phone numbers that are to be looked at? I was always under the impression that you needed to determine the array size first. And if that is the case, is there a way of looking at the files and figuring out what the array size should be?

    Sorry about my long code and stupid question...but I've decided to start taking comp sci courses in college and I'm a total noob when it comes to programming!

    Thanks everyone in advance!
     
  7. Dec 1, 2011 #6

    Mark44

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java-converting phone numbers into words
  1. Java - Word Counter (Replies: 16)

Loading...