Telephone Numbers

  • Write a program that reads in a series of numbers, as strings, from a file and converts them to valid telephone numbers.
  • The numbers are contained in file is called numbers.txt.
  • The numbers must read into the file using a loop, and stored as an array of Strings
  • Note that you can assume the length of the array to be 10.
  • As it reads in each number, it should convert each number to a valid telephone number in the format XXX.XXX.XXXX by calling a method named formatPhone.
  • Here is the information about this method:
    • The method is named formatPhone
    • It take in a String array variable
    • It alters the String array so that each String element is a properly formatted phone number, in the format XXX.XXX.XXXX
    • Hint, use a for loop to iterate through each String in the array
    • This method must call the substring method
    • It returns nothing.
  • The program must also contain a second method which writes out the contents of the array in a file named phones.txt
  • The second method:
    • The method is named print
    • It takes in an array of Strings
    • It opens up a new PrintWriter and connects it to a file called phones.txt
    • It prints out the array to the file using a for loop
    • It closes the PrintWriter
    • It returns nothing
  • Copy and paste the starter code below into a new file called Phones.java


/*
* @author
* Section info
*/
public class Phones {
    
    /**
    * Write your javadoc comment for formatPhone here
    */
public static void formatPhone(String[] numbers){
    return;
}

/**
* Write your javadoc comment for print here
*/
public static void print(String[] numbers){
    return;
}


public static void main(String[] args) {
    //write the program logic here


}


  • Copy and paste the below data into a file called numbers.txt:

5556667777
3334445555
1118884944
3332225677
1234445555
8889990000
1116664344
2224563333
1112229999
4568899399

  • Below is the output that your program should give inside of the phones.txt file
555.666.7777
333.444.5555
111.888.4944
333.222.5677
123.444.5555
888.999.0000
111.666.4344
222.456.3333
111.222.9999
456.889.9399
Once you have the correct output inside of the phones.txt file, your program is complete.


Customers
  • Write a program that reads in a series of names, along with their gender, from a file.
  • The file is called names.txt.
  • The program uses a loop to read in each name from the file and store each person's title (Mr. or Ms.) + name in an array called customers.
  • Note: you can assume that the length of the array is know ahead of time to be 8.
  • The program should then call two methods.
  • The first method:
    • The method is named capitalizeArray
    • It take in a String array
    • It alters the values in the String array to capitalize all of the letters
    • This method must use a for loop. It cannot call any outside methods that we have not discussed in this class.
    • It returns nothing.
  • The second method:
    • The method is named print
    • It takes in an array of Strings
    • It opens up a new PrintWriter and connects it to a file called customers.txt
    • It prints out the array to the file using a for loop
    • It closes the PrintWriter
    • It returns nothing
  • Once you have filled the customers array with titles and names, call the capitalizeArray method to capitalize all the names in the array.
  • Next, call the print method to display each of the capitalized names in the customers.txt file
  • Copy and paste the starter code below into a new file called Customers.java
/**
 * @author
 *
 */
import java.util.Scanner;
public class Customers
{
    /**
     * Write the javadoc comment for capitalizeArray here
     */
    public static void capitalizeArray(String[] customers)
    {
        return;
    }
    
    /**
     * Write the javadoc comment for printArray here
     */
    public static void printArray(String[] customers)
    {
        return;
    }
    
    public static void main(String[] args)
    {
        //declare your customers array here
        //note, you can assume your array will be of length 8
        String name, gender;
        
        
        //Use a while loop to read in the data from the file
        
        
        //call your two methods
    }
}

  • Copy and paste the below data into a file called names.txt:
Barbara Stanton
F
Timoteo Martinez
M
Grace Gu
F
Xing Xiao
M
Dung Kim
F
Tanya White
F
Alan Ngo
M
Abir Fadel
M
  • Below is the output that your program should give in a file called customers.txt:
MS. BARBARA STANTON
MR. TIMOTEO MARTINEZ
MS. GRACE GU
MR. XING XIAO
MS. DUNG KIM
MS. TANYA WHITE
MR. ALAN NGO
MR. ABIR FADEL


Customer Data: Phone Numbers
  • Assume you work for a company that tracks customer information, including name, gender and phone numbers
  • Your company has a file called customers.txt which contains the following information:

Jiming Wu
F
4082123458
James Brown
M
8315678432
Leanna Perez
F
4087654433
Xing Li
M
8313214555
Stacey Cahill
O
8312123333
Mohammed Abbas
M
4083134444
Kumari Chakrabarti
F
4086667777
Shakil Smith
M
4082123333
Jung Ahrin
F
8319257788
Pedro Martinez
M
4086162323
Ally Gu
O
4089256776
Tamara White
F
8317778978
Alvin Ngo
M
4089256677
Abir Fadel
M
8316645325
Brad Feinman
M
8312023443
Xiaohang Yue
M
8318990033
  • Your task is to read in the data from the above file using a loop, format the data, and write it into a new file called contacts.txt
  • When you are finished, contacts.txt should contain the following information:

Ms. Jiming Wu
Phone: (408) 212-3458
Mr. James Brown
Phone: (831) 567-8432
Ms. Leanna Perez
Phone: (408) 765-4433
Mr. Xing Li
Phone: (831) 321-4555
Mx. Stacey Cahill
Phone: (831) 212-3333
Mr. Mohammed Abbas
Phone: (408) 313-4444
Ms. Kumari Chakrabarti
Phone: (408) 666-7777
Mr. Shakil Smith
Phone: (408) 212-3333
Ms. Jung Ahrin
Phone: (831) 925-7788
Mr. Pedro Martinez
Phone: (408) 616-2323
Mx. Ally Gu
Phone: (408) 925-6776
Ms. Tamara White
Phone: (831) 777-8978
Mr. Alvin Ngo
Phone: (408) 925-6677
Mr. Abir Fadel
Phone: (831) 664-5325
Mr. Brad Feinman
Phone: (831) 202-3443
Mr. Xiaohang Yue
Phone: (831) 899-0033


  • In other words, whenever the customers.txt file contains an M below the customer name, the contacts.txt file should have the word Mr. before the name.
  • And, whenever the customers.txt file contains an F below the customer name, the contacts.txt file should have the word Ms. before the name.
  • Finally, whenever the file contains an O below the customer name, the contacts.txt file should have the word Mx. before the name.
  • In addition, the unformatted phone numbers in customers.txt must be formatted as (XXX) XXX-XXXX inside of contacts.txt.
Array requirements
  • You are required to store the names and phones in two separate - but parallel - arrays. In other words, the same index in each array should store the information about one unique customer. For example, index 0 in the names array should store the name of Jiming Wu and index 0 in the phones array should store her phone number.
  • You can assume for the purpose of the program that your arrays are both of length 16 when you declare them.
Method requirements
  • formatPhones Method:
    • The method is named formatPhones

    • It takes in an array of Strings as a parameter (unformatted phone numbers)

    • It alters each String to be a properly formatted phone number (XXX) XXX-XXXX

    • It cannot call any outside methods that we have not discussed in this class.

    • It returns nothing.

  • printArrays Method:
    • The method is named printArrays
    • It throws IOException
    • It takes in three parameters:
      •  the first and second parameters are arrays of Strings,
      • the third parameter is a String for the name of a text file in which to write out the data
    • The method must open the file whose name is passed in as a parameter
    • It declares a PrintWriter and uses it to write to the specified file
    • It uses a for loop to print out the contents of the array in the file, with each element on its own line, in the following format:
names[i]
Phone: phones[i]
    • It then closes the PrintWriter.
    • It returns nothing.
  • On the final, you will be required to write a the complete method, including its signature, and a Javadoc comment for each method
  • The method signature is provided below but will not be provided on the final.

  • Copy and paste the starter code into a new class file called PhoneContacts.java:
/*
 * @author
 * CIS 36A
 */
import java.io.*;
import java.util.Scanner;

public class PhoneContacts {
    
    public static void main(String[] args) throws IOException {
        String name, gender, phone;
        //Declare your two arrays here - one for names and one for phones
        
        //Declare File and Scanner
        //Below variable to keep track of array index in loop
        int i = 0;
    
        while(??????) {
            //read in name and gender
            
            //if statements related to gender
            
            //read in phone
            
            //store phone number in phones array
            
        }
        //call formatPhones method
        
        //call printArrays method
        //close Scanner
        
    }
   
          //write Javadoc comment here
    public static void formatPhones(String[] phones) {
        return;
    }
   
          //write Javadoc comment here
    public static void printArrays(String[] names, String[] phones, String fileName){//throw IOException
       return;
    }
}