Question

Lab 1.4: Arrays, File I/O and Method Review (40 pts) Assume you work for a company...

Lab 1.4: Arrays, File I/O and Method Review (40 pts)

  • Assume you work for a company that received a list of email addresses of potential new customers from a data broker.

  • Your company receives a file named customers.txt with the below information:

Jiming Wu

[email protected]

James Brown

[email protected]

Leanna Perez

[email protected]

Xing Li

[email protected]

Stacey Cahill

[email protected]

Mohammed Abbas

[email protected]

Kumari Chakrabarti

[email protected]

Shakil Smith

Shakattaq2G.com

Jung Ahrin

[email protected]

Pedro Martinez

[email protected]

Ally Gu

[email protected]

Tamara White

[email protected]

Alvin Ngo

[email protected]

Abir Fadel

greatabs2sbcglobal.net

Brad Feinman

[email protected]

Xiaohang Yue

[email protected]

  • Half of the 16 customers in this file have an email address that ends in .com.

  • Half have an email address that ends in .net

  • Write a program that reads in all of the customer information using a loop, and stores the names and email addresses in two String arrays

  • One array (length of 8) should store the customer information for customers whose emails that have a .com extension and one array (length 8) should store the customer information for customers whose email addresses with the .net extension.

  • The customer information should be stored in the arrays according to the formula:

    name : email

  • Additionally, note that a couple of email addresses in the file are not valid because they do not contain an @ symbol.

  • Each email should be tested to see if it is a valid email by calling the containsAt method, described below.

  • Any email address that does not contain an '@' should be labeled invalid by adding the word invalid in parenthesis next to the email. For example.

    Abir Fadel: greatabs2sbcglobal.net (invalid)

  • Finally, you will be required to write the two arrays to a file.

  • Each array should be written to a different file.

  • The .com array should be written to a file called com.txt

  • The .net array should be written to a file called net.txt

  • You must write the arrays to the file by calling the printArray method.

  • Note that you will need to call printArray twice - once for each array.

Method requirements

  • containsAt Method:

    • The method is named containsAt

    • It take in a String parameter for an email address

    • It determines whether the email address contains an '@'

    • This method must use a for loop. It cannot call any outside methods that we have not discussed in this class. Only charAt, substring and length allowed.

    • It returns true if the email contains an '@' and false otherwise

  • printArray Method:

    • The method is named printArray

    • It throws IOException

    • It takes in two parameters:

      • the first parameter is an array of Strings,

      • the second 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

    • It then closes the PrintWriter.

    • It returns nothing.

Below is the output your program should give in the net.txt file:

Leanna Perez: [email protected]

Xing Li: [email protected]

Kumari Chakrabarti: [email protected]

Jung Ahrin: [email protected]

Pedro Martinez: [email protected]

Tamara White: [email protected]

Abir Fadel: greatabs2sbcglobal.net (invalid)

Xiaohang Yue: [email protected]

Below is the output your program should give in the com.txt file:

Jiming Wu: [email protected]

James Brown: [email protected]

Stacey Cahill: [email protected]

Mohammed Abbas: [email protected]

Shakil Smith: Shakattaq2G.com (invalid)

Ally Gu: [email protected]

Alvin Ngo: [email protected]

Brad Feinman: [email protected]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the Java code to perform the use-case as per given scenario. The program reads the input file customers.txt and segregates the emails as directed into two arrays.

It then writes both the arrays to two separate files. Comments in the code could be referred for better understanding.

Code:

import java.io.BufferedReader;

import java.io.File;

/* Following import not required now

* as we are now throwing a parent

* exception for FileNotFound that is IOException.

*

*

* import java.io.FileNotFoundException;

*/

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

public class EmailIDSegregationDemo {

    /* Adding throws clause to main as we have

     * removed the try-catch. This class would not

     * compile without a try-catch or throws.

     */

    public static void main(String[] args) throws IOException {

        String[] comArr = new String[8];

        String[] netArr = new String[8];

        File inputFile = new File("customers.txt");

        // Initializing br here as try-catch removed

        BufferedReader br = new BufferedReader(new FileReader(inputFile));

        String name = br.readLine();

        String email = br.readLine();

        String info = "";

        // try {

            int comCount = 0;

            int netCount = 0;

            /* Repeatedly reading the contents of the input file

             * customers.txt, two lines at a time to get both

             * name and email of the customer.

             */

            while(email != null && name != null) {

                // Added the following Sys out for debugging purpose.

                // You could debug your own programs this way.

                // System.out.println(name + " " + email);

                

                /* Do not want to continue with a null entry

                 * so breaking out of the loop.

                 *

                 *

                 * =========================================

                 * Commenting this part as requested.

                 * For removing break, will read the lines outside

                 * the loop once, check them for null, and read two

                 * lines again near the end of each iteration to

                 * feed the condition for the next iteration.

                 

                if(email == null) {

                    break;

                }*/

                if(!containsAt(email)) {

                    email = email + " (invalid)";

                }

                info = name + " : " + email;

                if(email.contains(".com")) {

                    comArr[comCount] = info;                // Putting in the .com array

                    comCount++;

                } else if(email.contains(".net")) {

                    netArr[netCount] = info;                // Putting in the .net array

                    netCount++;

                }

                name = br.readLine();                       // Reading the next line as name

                email = br.readLine();                      // Reading the line after next line as email

            }

            br.close();

        /*} catch(FileNotFoundException fnfe) {

            fnfe.printStackTrace();

        } catch(IOException ioe) {

            ioe.printStackTrace();

        }*/

        //try {

            // Printing the arrays to the respective file.

            printArray(netArr, "net.txt");

            printArray(comArr, "com.txt");

        /*} catch(IOException ioe) {

            ioe.printStackTrace();

        }*/

    }

    /* The containsAt method.

     * Looping through the whole length

     * of the input email to look for @ char.

     */

    public static boolean containsAt(String email) {

        int len = email.length();

        for(int i=0; i<len; i++) {

            if(email.charAt(i) == '@') {

                return true;

            }

        }

        return false;

    }

    /* The print method to write

     * the segregated info to the respective file.

     * Iterating using for loop over the input array

     * and writing each element individually to the file.

     */

    public static void printArray(String[] userInfo, String outputFileName) throws IOException {

        PrintWriter pw = new PrintWriter(outputFileName);

        for(String info : userInfo) {

            pw.println(info);

        }

        pw.close();

    }

}

Add a comment
Know the answer?
Add Answer to:
Lab 1.4: Arrays, File I/O and Method Review (40 pts) Assume you work for a company...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer...

    Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer Data: Area Codes 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...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT