The file includes these names: CS 317 Program Assignment Name Arrange You are to
ID: 3727670 • Letter: T
Question
The file includes these names:
CS 317 Program Assignment Name Arrange You are to write a program that will read in each person's name and print out the results. The list has been collected form different places and the names are not in some standard format. A person's last name may appear last or it may appear first. A label has been associated with each name to identify where the last name appears. Either "surname" or "lastname" appears just before a person's last name. The other will be his first name followed by optionally middle initial or name. Capitalize the first letters of each part of a name. Examples of inputs are given below. Read in the names, and arrange them with the last name first, followed by a comma, followed by Input file: namesLast.txt Example input the first name and his middle name or initialsticexist tom archer lastname jones lastname luewey Huewey dewy See More surname Or-less Example output (Sorted): Jones, Tom Archer Luewey, Huewey Dewy Or-less, See MoreExplanation / Answer
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class ReadNames {
private String firstName,lastNAme;
/**
*
*/
public ReadNames() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param firstName
* @param lastNAme
*/
public ReadNames(String firstName, String lastNAme) {
super();
this.firstName = firstName;
this.lastNAme = lastNAme;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastNAme
*/
public String getLastNAme() {
return lastNAme;
}
/**
* @param lastNAme the lastNAme to set
*/
public void setLastNAme(String lastNAme) {
this.lastNAme = lastNAme;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return lastNAme + ", " + firstName ;
}
public static void main (String args[]){
try {
ArrayList<ReadNames> names = new ArrayList<>();
String filename = "/Users/sw007/Documents/workspace/test/src/march/namesLast.txt";
FileInputStream finputstream = new FileInputStream(filename);
DataInputStream dinputstream = new DataInputStream(finputstream);
BufferedReader br = new BufferedReader(new InputStreamReader(dinputstream));
String str;
while ((str = br.readLine()) != null) {
String[] strArray = str.split(" ");
boolean foundLastName = false;
ReadNames name = new ReadNames();
for (String st:strArray){
if (st.equalsIgnoreCase("lastname") || st.equalsIgnoreCase("surname")){
foundLastName = true;
continue;
}
if (foundLastName && name.getLastNAme() == null){
name.setLastNAme(st);
}
else{
if(name.getFirstName() != null){
name.setFirstName(name.getFirstName()+ " "+st);
}
else{
name.setFirstName(st);
}
}
}
names.add(name);
}
for (ReadNames name:names){
System.out.println(name.toString());
}
}
catch(Exception e){
System.out.println("No file exist with this name");
}
}
}
Sample Output:
Taylor, marie denise
thomas, james wilis
Brown, Stone Rock
lea, high lee
reynolds, stephens jabcobs
russell, mack hussell
Lewis, Michelle Tee
marshall, john mark
Moto, ah sey
vey, o e
knocktosee, Twosee ore
finitee, two N
ghigher, Eve N
jammer, T tow
one, Uh nuther
Lotts, lue Knew
moto, Ah C
phinshed, Just about
won, Thee last
* Please provide feedback and Thumbs up.Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.