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

1 (Sort names without duplicates) Write a program that reads the names from the

ID: 648565 • Letter: 1

Question

1 (Sort names without duplicates) Write a program that reads the

names from the all ten baby name files (provided), sorts all
names (boy and girl names together, duplicates removed), and
stores the sorted names in one file ten per line.

2 Write a program that prompts the user to enter the file name
one of the baby name file provided, reads the data from the
file, and stores the data in a new file without the rankings.
The new file is the same as the original file except that it
does not have the ranking for each row. The new file is named
as the input file with the extension .new.
Babyname files (10): 2004BabyName.txt,
2003BabyName.txt,

Explanation / Answer

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class BabyNames {
  
   public static void addName(ArrayList<String> names, String name){
       Object arr[] = (names.toArray());
       for(int i = 0; i < names.size(); ++i){
           if(arr[i].toString().compareTo(name) == 0){
               return;
           }
           else if(arr[i].toString().compareTo(name) > 0){
               names.add(i, name);
               return;
           }
       }
       names.add(names.size(), name);
   }
  
   public static void main(String args[]) throws IOException{
       Scanner in = new Scanner(System.in);
       ArrayList<String> names = new ArrayList<String>();
       for(int i = 0; i < 10; ++i){
           System.out.print("Enter input file name: ");
           String fileName = in.nextLine(), name = null;
           Scanner file = new Scanner(new FileInputStream(fileName));
           while(file.hasNext()){
               file.nextInt();
               name = file.next();
               addName(names, name);
           }
           file.close();
       }
       System.out.print("Enter output file name: ");
       String fileName = in.nextLine();
       BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
       Object arr[] = names.toArray();
       for(int j = 0; j < arr.length; ++j){
           bw.write(arr[j].toString() + " ");
           if((j + 1) % 10 == 0){
               bw.write(" ");
           }
       }
       bw.close();
   }
}


______________________________________________________________________________________

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class BabyNames {
  
   public static void main(String args[]) throws IOException{
       Scanner in = new Scanner(System.in);
       ArrayList<String> names = new ArrayList<String>();
       for(int i = 0; i < 10; ++i){
           System.out.print("Enter input file name: ");
           String fileName = in.nextLine(), name = null;
           String fileName1 = fileName.substring(0, fileName.length() - 4) + "new.txt";
           Scanner file = new Scanner(new FileInputStream(fileName));
           BufferedWriter bw = new BufferedWriter(new FileWriter(fileName1));
           while(file.hasNext()){
               file.nextInt();
               name = file.next();
               bw.write(name + " ");
           }
           file.close();
           bw.close();
       }
   }
}