Question

Using Java Write a program that reads a file of numbers of type int and outputs...

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.

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

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. 

Homework Answers

Answer #1

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 sc = new Scanner(new File("input.txt"));
       PrintWriter pw = new PrintWriter(new File("output.txt"));
       int prev = -1, num;
       num = sc.nextInt();
       while (sc.hasNext()) {
           //checking if it is not previous number
           if (num != prev) {
               pw.println(num);
           }
           //making current num as prev
           prev = num;
           //reading next num
           num = sc.nextInt();
       }
       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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I can open the file in the program, but I cannot figure out how to properly...
I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n' Below are the assignment details: This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a...
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
IN JAVA: Write code (using ArrayLists) to read a line of input from the user and...
IN JAVA: Write code (using ArrayLists) to read a line of input from the user and print the words of that line in sorted order, without removing duplicates. For example, the program output might look like the following: Type a message to sort: to be or not to be that is the question Your message sorted: be be is not or question that the to to
JAVA Write a program that will search a text file of strings representing numbers of type...
JAVA Write a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. Include appropriate error handling in case a line contains more than one int or it contains a String. Wrap all the work you are doing with the file in a try/catch/finally block with appropriate "catch" sections; the finally block will be where you close your file object (if desired, look this...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT