Hello I need help with finishing this program properly. Below is a small snippet
ID: 3741355 • Letter: H
Question
Hello I need help with finishing this program properly. Below is a small snippet of the overall instructions. What I need help with it is to add to the program where it MUST use a Formatter object to write out to a file. I also need to have it be able to tested with the test class provided below. Please show sample output of contents of code combinations writing out to a file.
THESE ARE INSTRUCTIONS
Write a program that, given a seven-digit number, uses a Formatter
object to write to a file every possible seven-letter word combination
corresponding to that number. There are 2,187 (37)(37) such combinations.
Avoid phone numbers with the digits 0 and 1.
THIS IS THE CODE IT NEEDS TO BE TESTED WITH
import java.util.Scanner;
import java.util.NoSuchElementException;
public class PhoneTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Phone application = new Phone();
System.out.print(
"Enter phone number (digits greater than 1 only): ");
try {
application.calculate(scanner.nextInt());IAM GETTING AN ERROR HERE BUT I NEED TO BE ABLE TO TEST THE CODE PROPERLY WITH THS CALCULATE
}
catch (NoSuchElementException elementException) {
System.err.println("Error inputting data.");
}
}
}
THIS IS THE WORKING CODE SO FAR
(IM NOT SURE IF MOST OF THESE HEADER FILES ARE REQUIRED FOR FORMAT OBJECT USE)
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Formatter;
import java.io.*;
import java.util.Scanner;
public class Phone
{
public static void main(String[] args)
{
// Scanner class
Scanner input = new Scanner(System.in);
// Array to store the digit
Character phoneNumbersInDigits[] = new Character[7];
// user input
String userNumber;
// Create one-dimensional arrays for letters according to digits except 0 and 1
char[] two = { 'A', 'B', 'C' };
char[] three = { 'D', 'E', 'F' };
char[] four = { 'G', 'H', 'I' };
char[] five = { 'J', 'K', 'L' };
char[] six = { 'M', 'N', 'O' };
char[] seven = { 'P', 'R', 'S' };
char[] eight = { 'T', 'U', 'V' };
char[] nine = { 'W', 'X', 'Y' };
// Create one-dimensional arrays to store the letters according to the
// digits position
char charAt0[] = new char[3];
char charAt1[] = new char[3];
char charAt2[] = new char[3];
char charAt3[] = new char[3];
char charAt4[] = new char[3];
char charAt5[] = new char[3];
char charAt6[] = new char[3];
// Initialize a count variable to count the number of phone numbers as strings
int count = 0;
// user to enter the phone number
System.out.print("Enter Phone Number:");
userNumber = input.nextLine();
//check user input a 0 or 1 and to check the dash after the third digit
for (int i = 0; i < userNumber.length(); i++) {
if (i != 3) {
if (!Character.isDigit(userNumber.charAt(i))) {
System.out.println("Invalid phone number format ###-####, # is a digit");
System.exit(0);
}
}
if (userNumber.charAt(i) == '0' || userNumber.charAt(i) == '1'
|| userNumber.charAt(3) != '-') {
System.out.println("Invalid phone number with 0 or 1 or without '-' in correct position");
System.exit(0);
}
}
int index = 0;
for (int i = 0; i < userNumber.length(); i++) {
if (i == 3)
continue;
else {
phoneNumbersInDigits[index] = userNumber.charAt(i);
index++;
}
}
// Compare the first digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[0].equals('2'))
System.arraycopy(two, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('3'))
System.arraycopy(three, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('4'))
System.arraycopy(four, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('5'))
System.arraycopy(five, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('6'))
System.arraycopy(six, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('7'))
System.arraycopy(seven, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('8'))
System.arraycopy(eight, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('9'))
System.arraycopy(nine, 0, charAt0, 0, charAt0.length);
// Compare the second digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[1].equals('2'))
System.arraycopy(two, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('3'))
System.arraycopy(three, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('4'))
System.arraycopy(four, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('5'))
System.arraycopy(five, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('6'))
System.arraycopy(six, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('7'))
System.arraycopy(seven, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('8'))
System.arraycopy(eight, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('9'))
System.arraycopy(nine, 0, charAt1, 0, charAt1.length);
// Compare the third digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[2].equals('2'))
System.arraycopy(two, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('3'))
System.arraycopy(three, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('4'))
System.arraycopy(four, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('5'))
System.arraycopy(five, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('6'))
System.arraycopy(six, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('7'))
System.arraycopy(seven, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('8'))
System.arraycopy(eight, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('9'))
System.arraycopy(nine, 0, charAt2, 0, charAt2.length);
// No need to compare 4th digits as its the '-'
// Compare the fifth digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[3].equals('2'))
System.arraycopy(two, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('3'))
System.arraycopy(three, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('4'))
System.arraycopy(four, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('5'))
System.arraycopy(five, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('6'))
System.arraycopy(six, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('7'))
System.arraycopy(seven, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('8'))
System.arraycopy(eight, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('9'))
System.arraycopy(nine, 0, charAt3, 0, charAt3.length);
// Compare the sixth digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[4].equals('2'))
System.arraycopy(two, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('3'))
System.arraycopy(three, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('4'))
System.arraycopy(four, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('5'))
System.arraycopy(five, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('6'))
System.arraycopy(six, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('7'))
System.arraycopy(seven, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('8'))
System.arraycopy(eight, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('9'))
System.arraycopy(nine, 0, charAt4, 0, charAt4.length);
// Compare the seventh digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[5].equals('2'))
System.arraycopy(two, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('3'))
System.arraycopy(three, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('4'))
System.arraycopy(four, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('5'))
System.arraycopy(five, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('6'))
System.arraycopy(six, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('7'))
System.arraycopy(seven, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('8'))
System.arraycopy(eight, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('9'))
System.arraycopy(nine, 0, charAt5, 0, charAt5.length);
// Compare the eighth digit of user entered phone number and copy the elements into another array
if (phoneNumbersInDigits[6].equals('2'))
System.arraycopy(two, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('3'))
System.arraycopy(three, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('4'))
System.arraycopy(four, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('5'))
System.arraycopy(five, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('6'))
System.arraycopy(six, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('7'))
System.arraycopy(seven, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('8'))
System.arraycopy(eight, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('9'))
System.arraycopy(nine, 0, charAt6, 0, charAt6.length);
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++) {
if (count < 10 || count > 2176) {
System.out.print(count + ": ");
System.out.print(charAt0[i]);
System.out.print(charAt1[j]);
System.out.print(charAt2[k]);
System.out.print(charAt3[l]);
System.out.print(charAt4[m]);
System.out.print(charAt5[n]);
System.out.print(charAt6[o]);
System.out.print(' ');
}
if (count == 2174)
System.out.println("...");
count++;
}
}
}
}
}
}
}
}
}
Explanation / Answer
I am giving a simpler code for your question. It generates an output file output.txt. YOU WILL NEED TO REFRESH THE PROJECT TO SEE THE FILE after running the program.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Phone.java
===========
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
public class Phone {
private char[][] letters = { {}, {}, //index 0 and 1 don't have any data
{'A', 'B', 'C'},
{'D', 'E','F'},
{'G', 'H', 'I'},
{'J', 'K', 'L'},
{'M', 'N', 'O'},
{'P', 'R', 'S'},
{'T', 'U', 'V'},
{'W', 'X', 'Y'}};
private int count;
private Formatter formatter;
public Phone() {
}
public void calculate(int number)
{
String s = "" + number;
char[] arr = s.toCharArray();
char[] combination = new char[7];
count = 0;
try {
formatter = new Formatter(new File("output.txt"));
nextCombination(arr, combination, 0);
System.out.println("There are total of " + count + " combinations");
System.out.println(count + " combinations written to file output.txt. Please check file");
formatter.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
//start implies the current position of digit to be changed. Recursive function to find combinations
private void nextCombination(char[] num, char[] combination, int start)
{
if(start == combination.length) //base condition, whenever we have reached past the length of array, we have found a new combination
{
count++;
formatter.format("%c%c%c%c%c%c%c ", combination[0], combination[1], combination[2],combination[3],combination[4],combination[5],combination[6]);
return;
}
int index = num[start] - '0'; //find the find of the digit , by subtracting ascii value of '0' from the number character '1' , '2' etc
for(int i = 0; i < 3; i++)
{
combination[start] = letters[index][i];
nextCombination(num, combination, start + 1);
}
}
}
output
=====
Enter phone number (digits greater than 1 only): 2345678
There are total of 2187 combinations
2187 combinations written to file output.txt. Please check file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.