Suppose you are given a text file that contains the names of people. Every name
ID: 3548263 • Letter: S
Question
Suppose you are given a text file that contains the names of people. Every name in the file consists of a first name and last name. Unfortunately, the programmer that created the file of names had a strange sense of humor and did not guarantee that each name was on a single line of the file. Read this file of names and write them to a new text file, one full name (both first name and last name) per line.
For example, if the input file contains
Bob Jones Fred
Charles Ed Marston Jeff Williams Ken Smith Tom Lorry
The output file should be
Bob Jones Fred Charles
Ed Marston
Jeff Williams
Ken Smith Tom Lorry
This is the code I have so far
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class ExtraCredit041{
public static void main(String[] args){
String fileName = "out.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName +
" contains the following lines: ");
try{
inputStream = new Scanner(new File(fileName));
}//Ending bracket of try block
catch(FileNotFoundException e){
System.out.println("Error opening the file " +
fileName);
System.exit(0);
}//Ending bracket of catch block
while(inputStream.hasNextLine()){
String line = inputStream.nextLine();
String[]outlist = line.split(" ");
for(int i =0; i<outlist.length;i++){
System.out.println(outlist[i]);
}//Ending bracket of for loop
}//Ending bracket of input while loop
inputStream.close();
//new code
String fileName = "out.txt"; //The name could be read from
//the keyboard.
PrintWriter outputStream = null;
try{
outputStream = new PrintWriter(fileName1);
}
catch(FileNotFoundException e){
System.out.println("Error opening the file " +
fileName1);
System.exit(0);
}
outputStream.println(line);
}
outputStream.close( );
System.out.println("Those lines were written to " +
fileName1);
}//Ending bracket of method main
}//Ending bracket of class ExtraCredit04
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class ExtraCredit041 {
public static void main(String[] args) {
String inputfileName = "/Users/Desktop/inputfile.txt";
String outputfileName = "/Users/Desktop/outputfile.txt"; // The name could be read from the keyboard.
Scanner inputStream = null;
PrintWriter outputStream = null;
String firstName = null;
String lastName = null;
ArrayList<String> al = new ArrayList<String>();
System.out.println("The file " + inputfileName + " contains the following lines: ");
StringBuffer sb = new StringBuffer();
String line = null;
String name = firstName = lastName = null;
try {
inputStream = new Scanner(new File(inputfileName));
while (inputStream.hasNextLine()) {
line = inputStream.nextLine();
sb.append(line);
sb.append(" ");
//System.out.println(sb);
}
line = sb.toString();
line = line.replaceAll("\s+", " "); // removing all the double space
System.out.println(line);
String[] outlist = line.split(" ");
if(outlist.length% 2 == 0){
for (int i = 0; i < outlist.length; i++) {
System.out.println(outlist[i].trim());
if(i%2 == 0){
firstName = outlist[i].trim();
}else{
lastName= outlist[i].trim();
if(firstName != null && firstName.length() > 0){
name = firstName + " " + lastName;
al.add(name);
firstName = null;
lastName = null;
}
}
}
}
// now write the output
outputStream = new PrintWriter(outputfileName);
for (String str : al){
outputStream.println(str);
}
System.out.println("Those lines were written to " + outputfileName);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
inputStream.close();
outputStream.close();
}// Ending bracket of method main
}// Ending bracket of class ExtraCredit04
The output file will contain :
Bob Jones
Fred Charles
Ed Marston
Jeff Williams
Ken Smith
Tom Lorry
Bob Jones Fred Charles
Ed Marston
Jeff Williams
Ken Smith Tom Lorry
This is because there should be some way to identify the firstname and lastname.
So the only way possible is to consider first word as firstname, second word as last name for a person...
Similarily 3rd and 4th word will be first and lastname for second person and so on....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.