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

Hi, I need help with a java programming project. specifically practice Program n

ID: 3778630 • Letter: H

Question

Hi, I need help with a java programming project. specifically practice Program number 2 on page 809 from chapter 10 of the 7th edition of the book , java:an introduction to problem solving and programming. it tells me to Write a program that reads a file of number of type int and outputs all the numbers to another file, but without
any duplicate numbers. Assume that the input file is sorted from smallest first to largest last. 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. For the text-file version, the file should be a text file with one number
per line. For the binary-file version, the file should be binary file that consists entirely of numbers of type int
that were written using writeInt.

I have written a program that technically runs , but i cant get it to create an output file that displays the needed numbers.

Here is my code

import java.lang.*;
import java.io.*; ///required packages
import java.util.*;

public class RemoveDuplicate
{
public static void main (String args []) throws Exception
{
int count = 0;

String line;

String newLine = System.getProperty("line.seperator"); //gets line seperator for system

System.out.print(" enter input file name"); // taking original file name from user

Scanner scanner = new Scanner(System.in);

String fileNameOld = scanner.next(); //reads original file name


System.out.print(" enter output file name"); // takes name of new file from user
  
  
String fileNameNew = scanner.next();
  
File file = new File (fileNameNew); // creates file object
  
PrintWriter out = new PrintWriter (new FileWriter(file)); //creates printwriter object
  
Scanner scanFile = new Scanner (new File(fileNameOld)); //creates scanner object for reading file
  
int temp = 0;
  
while(scanFile.hasNextLine()) // while numbers are in file
{
  
line = scanFile.nextLine(); // reads number
  
int number = Integer.parseInt(line);// converts numbers to int value


if(count == 0)
{
out.write(Integer.toString(number));
out.write(newLine);
temp = number;
  
count ++ ; //increment count
  
}
  
else
{
if (temp != number)
{
out.write(Integer.toString(number));
out.write(newLine);
temp = number;
}
}
}
out.close(); // closes stream
  
}
}

For reference I need to make it read an input txt/notepad file called numbers.txt which has numbers that look like following

and get it to produce an output file called output.txt, which looks like the following

Any help would immensly be appreciated.

12 33 33 178 239 1254 11245 x https://bb courses n x f Facebook x C Chegg Study IGuid X Hi, I Need Help Wit x C https://bb.courses.main /courses /1/1710. UMS03-C.0006 1/content/ 1873037 1/numbers.txt e.edu OM x D HMWK7 Screenshot x 5:34 PM 11/28/2016

Explanation / Answer

RemoveDuplicate.java


import java.lang.*;
import java.io.*; ///required packages
import java.util.*;

public class RemoveDuplicate
{
public static void main (String args []) throws Exception
{
int count = 0;

String line;

String newLine = System.getProperty("line.seperator"); //gets line seperator for system

System.out.print(" enter input file name"); // taking original file name from user

Scanner scanner = new Scanner(System.in);

String fileNameOld = scanner.next(); //reads original file name


System.out.print(" enter output file name"); // takes name of new file from user
  
  
String fileNameNew = scanner.next();
  
File file = new File (fileNameNew); // creates file object
  
PrintWriter out = new PrintWriter (new FileWriter(file)); //creates printwriter object
  
Scanner scanFile = new Scanner (new File(fileNameOld)); //creates scanner object for reading file
  
int temp = 0;
  
while(scanFile.hasNextLine()) // while numbers are in file
{
  
line = scanFile.nextLine(); // reads number
  
int number = Integer.parseInt(line);// converts numbers to int value


if(count == 0)
{
out.write(Integer.toString(number));
out.write(" ");
temp = number;
  
count ++ ; //increment count
  
}
  
else
{
if (temp != number)
{
out.write(Integer.toString(number));
out.write(" ");
temp = number;
}
}
System.out.println(number);
}
out.close(); // closes stream
  
}
}

Output:

enter input file nameD: umbers.txt
enter output file nameD:\output.txt
2
3
4
12
33
178
239
1254
11245

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote