Java-converting phone numbers into words

  • Comp Sci
  • Thread starter stripes
  • Start date
  • Tags
    Numbers
In summary, the conversation was about a program that converts phone numbers into words using a word bank. The code has been written and compiled, but when run, it does not seem to be functioning properly. Suggestions were made to use a debugger or embedded print statements to identify the issue.
  • #1
stripes
266
0
Java--converting phone numbers into words

Homework Statement



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.

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++;
}
}


}
}
 
Physics news on Phys.org
  • #2


Two comments:
1) When you post code, put it between [noparse]
Code:
 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.
 
  • #3


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

Sorry i will use the
Code:
 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.
 
  • #4


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:
Exception in thread "main" java.lang.NullPointerException
	at helloworld.main(helloworld.java:87)

Here is line 87:

Code:
//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.
 
  • #5


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

Code:
/**
 *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!
 

Related to Java-converting phone numbers into words

What is Java-converting phone numbers into words?

Java-converting phone numbers into words is a process of converting a sequence of numbers, typically a phone number, into a corresponding sequence of letters that spells out a word or phrase using Java programming language.

Why is there a need to convert phone numbers into words?

Converting phone numbers into words can be useful in situations where it is easier to remember a word or phrase instead of a long sequence of numbers. It can also be used for creating mnemonic devices or for data analysis purposes.

What are the steps involved in Java-converting phone numbers into words?

The steps involved in Java-converting phone numbers into words include: 1) Gathering the input phone number, 2) Parsing and separating the digits of the phone number, 3) Mapping the digits to their corresponding letters, 4) Combining the letters to form words or phrases, and 5) Outputting the converted phone number in words.

What are some challenges in converting phone numbers into words using Java?

One challenge in converting phone numbers into words using Java is handling special characters and spaces in the input phone number. Another challenge is creating a comprehensive mapping of digits to letters, as some letters may have multiple corresponding digits.

Are there any limitations to converting phone numbers into words using Java?

Yes, there are limitations to converting phone numbers into words using Java. One limitation is that the converted words may not always be easy to remember or make sense in a particular language. Also, the converted words may not always be unique, especially for longer phone numbers with repeating digits.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
18K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Sticky
  • Engineering and Comp Sci Homework Help
Replies
1
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top