- #1
stripes
- 266
- 0
Java--converting phone numbers into words
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.
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++;
}
}
}
}
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++;
}
}
}
}