Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Advice on removing duplicates from a .txt file while writing to an array

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Advice on removing duplicates from a .txt file while writing to an array

    Hi i am a beginner programmer and new this forum...i'm in the middle of an assignment and having some trouble with one part.....removing duplicates from a .txt file while writing to an array..here's my code so far..
    class MainProg{

    public static void main (String[]args){

    GenKeys keys = new GenKeys();

    try{
    String f = "keys.txt";
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);

    for (int i=1; i<=500; i++){
    //bw.write(i+ System.getProperty("line.separtor"));
    bw.write(keys.getrandom() + "\r\n");
    }

    // close the file after all the writing has taken place
    bw.close ();
    } catch (IOException e){
    System.out.println ("Error writing to file" + e.toString());
    }


    // declare a place to store each line as it is read in
    int num = 0;
    int myArray[] = new int [500];


    try{
    File file = new File("keys.txt");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
    System.out.println(scanner.next());



    }
    scanner.close();
    }catch(IOException e){
    System.out.print(e.toString());
    System.out.print("Non-Existant File");

    }

    }

    }


    next part ofmy question is
    MainProg should then read all of the keys from “keys.txt”, and store them in an
    array, removing any duplicates. (In other words if the number 55 is generated
    twice, only one of these 55’s should be added to the array). Note that your final
    array will contain less than 500 keys. Now, write the remaining keys into a file
    called “sorted.txt”, one key per line, with the smallest number on the first line
    and the largest file on the last line.

    If anyone can offer some help or advice on how to do this i would greatly appreciate this.

    Thanks

    --- Update ---

    here's my java class to return the random ints....

    import java.util.Random;

    public class GenKeys{
    Random random;

    public GenKeys(){

    random = new Random();

    }
    public int getrandom(){
    return random.nextInt(250)+ 1;

    }

    }


  2. #2
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Advice on removing duplicates from a .txt file while writing to an array

    You could use a HashMap, and generate strings.hashCode() as the key. This would eliminate duplicates. And to get an array Collections<String> c = hm.values()
    Object[] array = c.toArray()

Similar Threads

  1. Removing Duplicates not working ... new to java
    By jeichenlaubjr in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2013, 02:59 PM
  2. merging two unsorted arrays and removing the duplicates..help
    By d4divya2005 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:26 PM
  3. linked list printing empty after removing duplicates
    By mia_tech in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 11th, 2012, 08:57 AM
  4. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM
  5. advice for an Odd, Even, Zero counter from input.txt file
    By sanchezjk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 06:15 AM

Tags for this Thread