Comp Sci Java-converting phone numbers into words

  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
The discussion revolves around a Java program designed to convert phone numbers into words using a word bank. The user inputs file names for phone numbers and words, but the program fails to execute as intended, leading to confusion about its functionality. Key issues identified include improper handling of array sizes, leading to potential null pointer exceptions, and the need for better debugging practices, such as using print statements to trace execution. The user ultimately resolves the array size problem but continues to seek advice on improving the code's structure and functionality. The conversation emphasizes the importance of debugging and managing array sizes in programming.
stripes
Messages
262
Reaction score
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


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.
 


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.
 


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.
 


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!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • Sticky
  • · Replies 1 ·
Replies
1
Views
16K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K