Question

In: Physics

Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...

Using Java

Project 2: Deduplication

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.

This means that you should not use arrays, lists, arraylists,
linked lists, sets, maps, trees, or any other multi-element data structure.

Instead, your 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:

  1. a count of numbers in the input file
  2. a count of the numbers in the output file
  3. and the number of duplicates found in the input file

Submit program files for all classes, copies of your input and output text files, and a PrintScreen of the console output when your program is run.

Example:

  Input File             Output File  
  Contents               Contents
     1                      1
     3                      3
     3                      60
     3                      75
     60                     80
     60                     100
     75                     130
     75                     140
     75                     985
     80                     1000
     80
     100
     130
     140
     985
     985
     985
     1000

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.  

Please ensure that all of your methods (except main) are commented using Javadoc-style tags (@param and @return) and your algorithms adequately describe how your programs solve the Problem Statements.

Solutions

Expert Solution

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();
}
}


Related Solutions

Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT