For example, if the input file contains: Colette Sapienza Gretta Beumer Emmanuel
ID: 3552725 • Letter: F
Question
For example, if the input file contains:
Colette Sapienza
Gretta Beumer
Emmanuel Mossman
Colette Sapienza
Shawanda Hutt
Adriana Montilla
Adriana Montilla
Adriana Montilla
Marica Stanfill
Neva Shover
Tijuana Means
Adriana Montilla
Gerri Koenig
Kirsten Beres
Kirsten Beres
The output file must contain:
Beres, Kirsten
Beumer, Gretta
Hutt, Shawanda
Koenig, Gerri
Means, Tijuana
Montilla, Adriana
Mossman, Emmanuel
Sapienza, Colette
Shover, Neva
Stanfill, Marica
Your program must contain and use the following methods:
Sorts ArrayList names
You cannot use ArrayList
public static int find
(ArrayList<String> names, int i)
Let s be names.get(i);
Looks for s beginning from position i+1. If s is found at a position j (j > i), return j. Otherwise return -1.
public static void
removeDuplicates(ArrayList<String> names)
Removes duplicates from ArrayList names
public static void
sort(ArrayList<String> names)
Sorts ArrayList names
Explanation / Answer
Complete Program to copy and paste:
// SequenceOfNames.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class SequenceOfNames
{
// find method
public static int find(ArrayList<String> names, int i)
{
String s = names.get(i);
for(int j = i + 1; j < names.size(); j++)
{
if(names.get(j).equals(s))
return j;
}
return -1;
}
// removeDuplicates method
public static void removeDuplicates(ArrayList<String> names)
{
for(int i = 0; i < names.size(); i++)
{
int result = find(names, i);
if(result != -1)
{
names.remove(result);
i--;
}
}
}
// sort method
public static void sort(ArrayList<String> names)
{
for(int i = 0; i < names.size() - 1; i++)
{
for(int j = 1; j < names.size(); j++)
{
if(names.get(j).compareTo(names.get(j - 1)) < 0)
{
String temp = names.get(j - 1);
names.set(j - 1, names.get(j));
names.set(j, temp);
}
}
}
}
// main method
public static void main(String[] args)
{
ArrayList<String> names = new ArrayList<String>();
Scanner inFile = null;
PrintWriter outFile = null;
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of the input file: ");
String inFileName = input.next();
System.out.print("Enter the name of the output file: ");
String outFileName = input.next();
try
{
inFile = new Scanner(new File(inFileName));
}
catch(FileNotFoundException fnfe)
{
System.out.println("The input file " + inFileName + " is not found.");
fnfe.printStackTrace();
}
// read names from input file
while(inFile.hasNext())
{
String first = inFile.next();
String last = inFile.next();
String name = last + " " + first;
names.add(name);
}
// call to find method
int result = find(names, 0);
if(result != -1)
System.out.println("The name " + names.get(0) + " is found again at the position " + find(names, 0) + ".");
else
System.out.println("The name " + names.get(0) + " is not found again in the list.");
// call to removeDuplicates method
removeDuplicates(names);
// call to sort method
sort(names);
try
{
outFile = new PrintWriter(outFileName);
}
catch(FileNotFoundException fnfe)
{
System.out.println("The output file " + outFileName + " is not found.");
fnfe.printStackTrace();
}
// display the non-duplicate names in output file
for(int i = 0; i < names.size(); i++)
{
String[] strs = names.get(i).split(" ");
outFile.println(strs[0] + ", " + strs[1]);
}
inFile.close();
outFile.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.