简体   繁体   中英

Deduplication from text file using scanner without arrays (Java)

I'm trying to read a text file that contains integers on different lines that are already sorted from least to greatest. Said integers have to be transported to another text file but without any duplicates and without using any sort of arrays, array list, maps, sets, or any other sort of data structure.

Currently I've tried to read numbers from the first text file,and use a while loop to check if the next numbers are similar, with the Scanner. The only problem is that the loop gets the next integer in it as well so this algorithm only works if all numbers are duplicated. This'll make my day if somebody could at least point me in the right direction, thanks in advance!

Example Text File One (all integers are on a new line): 5 5 5 5 5 8 8 9 9 9 9 10 10 11

My output: 5 8 9 10 Expected Output: 5 8 9 10 11

public static void deduplicateFiles(String inputFileName,String outputFileName){
    Scanner scan = null;
    try{
        scan = new Scanner(new FileInputStream(inputFileName) );
    }catch(FileNotFoundException e){
        System.out.println(e.getMessage() );
    }
    PrintWriter writer = null;
    try{
        writer = new PrintWriter(outputFileName);
    }catch(FileNotFoundException e){
        System.out.println(e.getMessage());
    }
    while(true){ 
        int firstInt = scan.nextInt();
       scan.nextLine();
       //if(scan.nextInt() != firstInt)
        int counter = 1;

        while(scan.nextInt() == firstInt && scan.hasNext() != false){
                System.out.println("counter"  +counter);
                counter++;
                scan.nextLine();
        }
        System.out.println("The integers:" + firstInt);
        writer.println(firstInt);
        if(scan.hasNext() == false)
            break;
    }    
    writer.flush();
    writer.close();
}

Read all the integers from the file first. Add them to a Set and then write from the set to a file. You can use LinkedHashSet to keep the order.

I've modified the second part of your code, adding a comparison between numbers to the variable 'nextInt'. It was mainly about entries in the right places, I hope it resolve your problem:

public void deduplicateFiles (String inputFileName, String outputFileName){
    Scanner scan = null;
    try {
        scan = new Scanner(new FileInputStream(inputFileName));
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(outputFileName);
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
    while (true) {
        //this two lines are initial
        int firstInt = scan.nextInt();
        int counter = 1;

        //next lines are compare adjacent values
        while (scan.hasNextLine()) {
            int nextInt = scan.nextInt();
             if (nextInt == firstInt) {
                counter++;
            } else {
                System.out.println("counter " + counter);
                counter = 1;
                System.out.println(firstInt);
                 writer.println(firstInt);
                 firstInt = nextInt;
            }
        }

        //this three lines terminate adding
        writer.print(firstInt);
        System.out.println("counter " + counter);
        System.out.println(firstInt);

        break;

    }
    writer.flush();
    writer.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM