-1

I have an external file with some random numbers but some are duplicates. I need to write a program that reads the numbers and prints out all the numbers that there are. Then I need to write a program that then prints all the numbers without their duplicate this is what i have so far. I apologize if my formatting is not right on this question. I am new to Stack overflow.

 import java.io.*;
   import java.util.Scanner;
   public class main
{
    public static void main (String [] args)throws Exception
    {
        Scanner sf = new Scanner(new FileInputStream("prog415h.dat"));
        int[] number = new int[100];
        int count = 0;
        Runner runner = new Runner();
        while(sf.hasNextLine())
        {
            number[count] = sf.nextInt();

            System.out.print("The orginal set of numbers are: " + number[count] + " ");
            count++;


        }
        runner.Repeat();
    }
}

public class Runner
{
    public static int[] numbers;
    public static void setNumbers(int[] numbers)
    {
        Runner.numbers = numbers;
    }
    public static void Repeat()
    {
        for (int i = 0; i < 30; i++) 
        { 
            int x = numbers[i]; 
            boolean good = true; //any duplicates found? 
            for (int j = 0; j < 30; j++) 
            { 
                if (i != j) //dont compare it to itself 
                { 
                    if (x == numbers[j]) // duplicate 
                    { 
                        good = false; 
                        System.out.print("");
                    } 
                } 
            } 


        } 
    }
}
Bailey Spell
  • 43
  • 1
  • 7

2 Answers2

0

You can modify your code using collections. It would be way simpler.

 public static void main (String [] args)throws Exception
        {
            Scanner sf = new Scanner(new FileInputStream("prog415h.dat"));
    int[] number = new int[100];
    int count = 0;
    Runner runner = new Runner();
            while(sf.hasNextLine())
                {
            number[count] = sf.nextInt();

            System.out.println("The orginal set of numbers are: " + number[count] + " ");
            count++;
        }
        runner.setNumbers(number);
        runner.Repeat();
    }
}

class Runner
{
    public static int[] numbers;
    public static void setNumbers(int[] numbers)
    {
        Runner.numbers = numbers;
    }
    public static void Repeat()
    {
        Set<Integer> uniqueElements = new HashSet<Integer>();
        System.out.println("List of Unique elements"); 
        for (int i = 0; i < numbers.length; i++) 
        { 
            if(!uniqueElements.contains(numbers[i]))
            {
                uniqueElements.add(numbers[i]);
                System.out.println(numbers[i]);
            }
        } 
        System.out.println();
    }
}
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • Thank you! but i have to keep them in two classes. How would i do that? – Bailey Spell Mar 04 '16 at 14:06
  • @BaileySpell You want to keep them in 2 different classes and use Array or Collection(List) will do? – Kishore Bandi Mar 04 '16 at 14:14
  • @BaileySpell I've updated the code snippet as per your requirement. The Runner Class will print all unique numbers, no repetitions will occur. Used **HashSet because it'll be faster to process than using 2 different array loops** to check if the duplicate number is already printed or not. Hope this helps. Also, in the output you'll see an additional 0 as well, this is because you're using primitive type int as Array, so there will always be a value 0 in it. You can stop this by Either changing it to `Integer and checking for null` or by `adding > 0 condition in the contains` if condition. – Kishore Bandi Mar 04 '16 at 14:32
  • Thank you for your help! – Bailey Spell Mar 04 '16 at 15:31
  • @BaileySpell If its resolved, Can you close out the question by accepting the answer. – Kishore Bandi Mar 04 '16 at 15:38
0

A LinkedHashSet should do the trick

// create a hash set
LinkedHashSet<Integer> hs = new LinkedHashSet<>();
// Add items
hs.add(9);
hs.add(2);
hs.add(3);
hs.add(4);
hs.add(5);
hs.add(1);
hs.add(6);
hs.add(7);
hs.add(1);
hs.add(2);
System.out.println(hs);
// Prints [9, 2, 3, 4, 5, 1, 6, 7]
// i.e. preserves order and only one copy of each item
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75