In: Computer Science
Using Java
Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should also be sorted from smallest to largest with one number on each line.
Your program should not assume that there is a fixed number of entries to be read, but should be able to work with any number of entries in both the input and output files.
- Do not use arrays, lists, arraylists, linked lists, sets, maps, trees, or any other multi-element data structure.
program should read in and write out numbers from the input and output files at the same time, eliminating duplicates from the output file as you go.
Your program should obtain both file names from the user. For the original (input) file, create a text file that stores one number per line with several duplicates in sorted order. The output file should be created by your program. When completed, your program should display on the console:
|
Sample console dialog, where input from the user is underlined in italics
Enter input file name or full path: numbers.txt Enter output file name or full path: output.txt There were 18 numbers input, 10 output, and 8 duplicates. |
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class RemoveDupInFile {
public static void main(String[] args) throws
Exception {
// opening file to read
Scanner sc1 = new
Scanner(System.in);
System.out.println("Enter input
file name or full path: ");
String iFileName =
sc1.nextLine();
System.out.println("Enter output
file name or full path: output.");
String oFileName =
sc1.nextLine();
int inputCount = 1, outputCount =
1;
Scanner sc = new Scanner(new
File(iFileName));
PrintWriter pw = new
PrintWriter(new File(oFileName));
int prev = -1, num;
num = sc.nextInt();
while (sc.hasNext()) {
// checking if
it is not previous number
inputCount++;
if (num != prev)
{
outputCount++;
pw.println(num);
}
// making
current num as prev
prev =
num;
// reading next
num
num =
sc.nextInt();
}
System.out.println("There were " +
inputCount + " numbers input, " + outputCount + " output, and
"
+ (inputCount - outputCount) + " duplicates.
");
pw.println(num);
pw.close();
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me