Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that processes a text data file of names in which each name is o

ID: 3933969 • Letter: W

Question

Write a program that processes a text data file of names in which each name is on a separate line of at most 80 characters. See two sample names below: Hartman-Montgomery, Jane R. Doe, J. D. On each line the surname is followed by a comma and a space. Next comes the first name or initial, then a space and the middle initial. Your program should scan the names into three arrays - surname, first, and middle_init. If the surname is longer than 15 characters, store only the first 15. Similarly, limit the first name to ten characters. Do not store periods in the first and middle_init arrays. Write the array’s contents to a file named names_output.txt, aligning the contents of each column, as shown below for the sample data above:

Hartman-Mongom Jane R
Doe J D

Explanation / Answer

PROGRAM CODE:

package sample;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.regex.Pattern;

public class NameProcessor {

   public static void main(String args[])

   {

       String[] surname = new String[10];

       String[] first = new String[10];

       String[] middle_init = new String[10];

       int counter = 0; //counter for the array

      

       //Reading the names file and storing the data

       // using buffered reader to read from the names_input.txt file

       System.out.println("INPUT NAMES: ");

       try(BufferedReader br = new BufferedReader(new FileReader("names_input.txt"))) {

          for(String line; (line = br.readLine()) != null; ) {

       //Storing each line of the file into a string called line and then splitting the line based on space

       //after splitting the different words get stored in the names array

             System.out.println(line);

             String[] names = line.split(Pattern.quote(" "));

             // now the first index folds the last name, the second index holds the first name and the last index holds the middle name

             surname[counter] = names[0].trim();

             first[counter] = names[1].trim();

             middle_init[counter] = names[2].trim();

             counter++;

          }

          br.close();

          // line is not visible here.

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       //Processing the names

       for(int i=0; i<counter; i++)

       {

           int lengthOfSurname = surname[i].length();

           //removing the comma

           if(surname[i].contains(","))

               surname[i] = surname[i].substring(0, lengthOfSurname-1);

           lengthOfSurname--;

           // removing the period for middle and first names

           if(middle_init[i].contains("."))

               middle_init[i] = middle_init[i].substring(0, middle_init[i].length()-1);

           if(first[i].contains("."))

               first[i] = first[i].substring(0, first[i].length()-1);

          

           //checking if the surname length is > 15

           if(lengthOfSurname>15)

           {

               surname[i] = surname[i].substring(0, 15); // index starts from 0

           }

          

       }

      

       //Printing the output

       System.out.println(" OUTPUT NAMES: ");

       for(int i=0; i<counter; i++)

       {

           System.out.println(surname[i] + " " + first[i] + " " + middle_init[i]);

       }

   }

}

OUTPUT:

INPUT NAMES:

Hartman-Montgomery, Jane R.

Doe, J. D.

OUTPUT NAMES:

Hartman-Montgom Jane R

Doe J D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote