The Artima Developer Community
Sponsored Link

Java Answers Forum
Request help with number sort/algor. # 2

1 reply on 1 page. Most recent reply: Feb 28, 2003 7:09 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Mark Schuh

Posts: 8
Nickname: shoe2
Registered: Feb, 2003

Request help with number sort/algor. # 2 Posted: Feb 28, 2003 3:25 PM
Reply to this message Reply
Advertisement
WHOOPS: I forgot the code, sorry about that

Greetings,

I am very new to Java.

Below is a program that will copy the data (numbers) of one file to another new file. It requests the name of each file. It works fine. I am now trying to copy over to the new file--only one of each number--in other words I want to eliminate repeated numbers. If my source file consists of these numbers:
1, 5, 7, 7, 8, 9, 9, 9, 22, 34 -9sentinel I only want to copy over the 1, 5, 7, 8, 9, 22, and 34. (The -9 is taken care of)

Can anyone lead me in the right direction on how I can solve this problem to get rid of repeated numbers?

One other question: Are my Exceptions satisfactory or is there a better way to incorporate them?

Any help would be greatly appreciated.

Best Wishes

Mark Schuh


import java.io.*;

class FileCopyMaster
{
public static void main(String[] args)
{

String sourceFile = null;
String brandnewFile = null;

try
{
System.out.println("Enter the file you want to copy");
sourceFile = SavitchIn.readLineWord();

System.out.println("Enter the file you want to copy");
brandnewFile = SavitchIn.readLineWord();

File fileIn = new File(sourceFile);
File fileOut = new File(brandnewFile);

FileInputStream streamIn = new FileInputStream(fileIn);
FileOutputStream streamOut = new FileOutputStream(fileOut);

int c;
while ((c = streamIn.read()) != -1)
{
streamOut.write(c);
}

streamIn.close();
streamOut.close();
}
catch (FileNotFoundException e)
{
System.err.println("FileCopy: " + e);
}
catch (IOException e)
{
System.err.println("FileCopy: " + e);
}
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Request help with number sort/algor. # 2 Posted: Feb 28, 2003 7:09 PM
Reply to this message Reply
One way is to stick 'em all in a Set; that will eliminate any duplicates.

Another way is to keep track of the last value written or sent or whatever and only write the current one if it is different from the last one.

Flat View: This topic has 1 reply on 1 page
Topic: File I/O  questions Previous Topic   Next Topic Topic: Help changing a JPanel background color

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use