Question

Write a program that processes a data file of names in which each name is on...

Write a program that processes a data file of names in which each name is on a separate line of at most 80 characters. Here are two sample names:

Hartman-Montgomery, Jane R.

Doe, J. D.

Strings 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, aligning the contents of each column:

Hartman-Montgom Jane R

Jane R Doe J D J D

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

package Abc;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.*;

public class allmain {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

List<User> usrlst = new ArrayList<User>();

try{

BufferedReader br=new BufferedReader(new FileReader(new File("userinfo.txt")));

BufferedWriter bw=new BufferedWriter(new FileWriter(new File("userinfoRes")));

String line="";

while((line=br.readLine())!=null){

String arr[] = line.split(" ");

String mxs = "";

String mxf = "";

String mxm = "";

if(arr[0].length()>15){

mxs = arr[0].substring(0,14);

}else

mxs=arr[0];

if(arr[1].length()>10){

mxf = arr[1].substring(0,10);

}else

mxf =arr[1];

if(arr[2].length()>10){

mxm = arr[2].substring(0,10);

}else

mxm=arr[2];

String sname = mxs;

String fname = mxf;

String mname = mxm;

usrlst.add(new User(sname,fname,mname));

}

for(User u:usrlst){

System.out.println(u.toString());

bw.write(u.toString());

bw.newLine();

}

bw.close();

br.close();

}catch(Exception e){

System.out.println("Error in reading file:"+e);

}

}

}

output:

Hartman-Montgo Jansfsfsfs R.
Doe, J. D.

screenshots:

input file:

output file:

comments;

the above code is executable one with output screenshots.

input is read from file and output is just printed to console and writern to file

please change path of my file and replace ur path for successfull reading and writng files.

Add a comment
Know the answer?
Add Answer to:
Write a program that processes a data file of names in which each name is on...
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
  • C language

    In C language please. Write a program that processes a data file of names in which each name is on a separate line of at most 80 characters. Here are two sample names: 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...

  • C++ Do not use "cin" to get the names from the user. Use "getline()". Name Arranger...

    C++ Do not use "cin" to get the names from the user. Use "getline()". Name Arranger Write a program that asks for the user’s first, middle, and last names. The names should be stored in three different character arrays. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example,...

  • i have to write a program that asks the names of 2 files. the first should...

    i have to write a program that asks the names of 2 files. the first should be open for reading second for writing. the program should read the the contents of the first file change all characters to uppercase and store in the second file. the second file will be a copy of the first file, except that all characters will be uppercase. use notepad to test the program. i got this so far {         String firstfile;         String...

  • In C Write a function that asks for the user's first, middle, and last names. The...

    In C Write a function that asks for the user's first, middle, and last names. The names will be entered by the user on a single line and will be stored in three different C-strings. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example, if the user entered...

  • C++ A professor plans to store the following data for each of his students: Last Name,...

    C++ A professor plans to store the following data for each of his students: Last Name, First Name, Class Average (as a double), Letter Grade (“A”, “A-“, “B+”, “B”, etc.) Write two programs for writing and reading such a file, with the user choosing the file name. Store the (int) number of students as the first data value in the file, followed by \n. Then use the following delimiter scheme: Last Name(comma)First Name(comma)Average(space)Letter Grade(newline) For the file writer program, input...

  • java Program Write a program called Copy that accepts the names of an input file and...

    java Program Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt

  • Write a program to read a text file, place each line it reads into an array....

    Write a program to read a text file, place each line it reads into an array. Then print the array’s contents one line at a time. Then add to the end of the text file “Success”. Use error exception handling and if the text file does not exist print "Error file not found." Always print "It worked." as part of the try clause. Upload a zip folder file of the .py and text file. roses are roses are red, violets...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

  • Write a program that prompts the user to enter a list of names. Each person's name...

    Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space (: and the names are entered lastName, firstName (i.e. separated by ',). Your program should then print out the names, one per line, with the first names first followed by the last names. A sample run of your program should look like Please enter your list of names: Epstein, Susan; St. John, Katherine;...

  • java Write a program called Copy that accepts the names of an input file and an...

    java Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt

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