Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a CopyInts program to copy a file of integers to an output file after processing those integers: Write a program that reads a file

write a CopyInts program to copy a file of integers to an output file after processing those integers:

Write a program that reads a file of numbers of type int into an int array and then writes all the numbers to another file, but without any duplicate numbers.

Do not assume that the numbers in the input file are ordered from smallest to largest, so you should start by sorting the array of ints. That will allow you to easily check for duplicates.

After the program is run, the new file will contain all the numbers in the original file, but no number will appear more than once in the file. The numbers in the output file should also be sorted from smallest to largest.

Your program should obtain both file names from the user. You can assume that there is one number per line.

Hints:

Use the IO.readAllInts method to easily read in the original set of ints, then use Arrays.sort to sort the array before processing the numbers to remove duplicates.

Check for errors: null returned by IO.readAllInts or the int array returned has 0 length.

Once you have sorted the array, you need to process its elements in a for-each loop and keep track of the last number you wrote to the output file in order to detect whether the next int in the array is a duplicate. Update the last number written each time you write out a number.

How do you make sure you write out the first int? Set the last number written int variable equal to the first element of the array minus 1 so that the first number in the array will not appear to be a duplicate.

Note: When you use println to write an int to the output file, it automatically converts that int to a String.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago