1

Can't get this working, it's supposed to sort any number of numbers input by a user and then eliminate duplicates. Right now the program is only printing 0's, but it should be printing an array with no duplicates. I have to do this the hard way as well, I can't use Java's built in sorting or array copying methods. Why does my code only print 0's, and how can I fix it?

package Lab_10;

import java.util.Scanner;
public class Eliminating_duplicates
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of numbers: ");
        int numOfNums = input.nextInt();
        int[] list = new int[numOfNums];
        int[] newList = new int[numOfNums];
        for( int x = 0; x < list.length; ++x)
        {
            while(numOfNums != -1 && x < list.length)
            {
                System.out.print("Enter value " + (x + 1) + ": ");
                int value = input.nextInt();
                list[x] = value;
                ++x;
            }
        }
        sortList(list);
        System.out.println("Here is the sorted list: ");
        for (int x = 0; x < list.length; ++x)
        {
            System.out.println(list[x]);
        }
        eliminateDuplicates(list);
        System.out.println("Here is the list without duplicates: ");
        for (int x = 0; x < newList.length; ++x)
        {
            System.out.println(newList[x]);
        }
    }
    public static void sortList(int[] list)
    {
        int temp;
        boolean madeASwap = true;
        int lastIndex = list.length-1;
        while (madeASwap)
        {
            madeASwap = false;
            for (int x = 0; x < lastIndex; ++x)
            {
                if (list[x] > list[x + 1])
                {
                    temp = list[x];
                    list[x] = list[x + 1];
                    list[x + 1] = temp;
                    madeASwap = true;
                }
            }

        }
    }
    public static int[] eliminateDuplicates(int[] list)
    {
        int end = list.length;
        for (int i = 0; i < end; i++)
        {
            for (int j = i + 1; j < end; j++) {
                if (list[i] == list[j]) {
                    for (int k = j + 1; k < end; k++, j++) {
                        list[j] = list[k];
                    }
                    --end;
                    --j;
                }
            }
        }
        int[] newList = new int[end];
        return newList;
    }
}
1
  • As a side note: You should debug through your code to really understand what it is doing. For instance: in main the nested loop has some redundancy. for ( int x = 0; x < list.length; ++x) will only execute once because the nested while(numOfNums != -1 && x < list.length) { ... ++x; } will handle all iteration.
    – OhBeWise
    Commented Apr 17, 2015 at 18:03

3 Answers 3

1

You don't actually copy the values over.

int[] newList = new int[end];
return newList;

This creates a new list containing only zeroes, which gets ignored. You can copy them over using something like:

int[] newList = new int[end];
for (int i = 0; i < end; i++) {
  newList[i] = list[i];
}
return newList;

The reason you think this works, I believe, is that you also have an array called newList in your main method, but you never assign anything to it.

int[] newList = new int[numOfNums];

This never gets accessed again until you try to print it's contents, which are, again, by default, all zeroes.

Make sure to save the returned array by doing this:

newList = eliminateDuplicates(list);

None of this matters, though, because your modification of the array is destructive, which means that your array is different after calling the eliminate duplicates method. Here's the fixed program:

import java.util.Scanner;

public class Eliminating_duplicates {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of numbers: ");
    int numOfNums = input.nextInt();
    int[] list = new int[numOfNums];
    int[] newList = new int[numOfNums];
    for (int x = 0; x < list.length; ++x) {
      while (numOfNums != -1 && x < list.length) {
        System.out.print("Enter value " + (x + 1) + ": ");
        int value = input.nextInt();
        list[x] = value;
        ++x;
      }
    }
    sortList(list);
    System.out.println("Here is the sorted list: ");
    for (int x = 0; x < list.length; ++x) {
      System.out.println(list[x]);
    }
    newList = eliminateDuplicates(list);
    System.out.println("Here is the list without duplicates: ");
    for (int x = 0; x < newList.length; ++x) {
      System.out.println(newList[x]);
    }
  }

  public static void sortList(int[] list) {
    int temp;
    boolean madeASwap = true;
    int lastIndex = list.length - 1;
    while (madeASwap) {
      madeASwap = false;
      for (int x = 0; x < lastIndex; ++x) {
        if (list[x] > list[x + 1]) {
          temp = list[x];
          list[x] = list[x + 1];
          list[x + 1] = temp;
          madeASwap = true;
        }
      }

    }
  }

  public static int[] eliminateDuplicates(int[] list) {
    int end = list.length;
    for (int i = 0; i < end; i++) {
      for (int j = i + 1; j < end; j++) {
        if (list[i] == list[j]) {
          for (int k = j + 1; k < end; k++, j++) {
            list[j] = list[k];
          }
          --end;
          --j;
        }
      }
    }
    int[] newList = new int[end];
    for (int i = 0; i < end; i++) {
      newList[i] = list[i];
    }
    return newList;
  }
}
1
  • Thank you very much, very well explained I understand it clearly now.
    – Wrath
    Commented Apr 17, 2015 at 18:22
1

you need to include this to return the proper int array in your eliminateduplicates function:

   int[] newList = new int[end];
    for (int l = 0; l < end; l++) {
        newList[l] = list[l];
    }
    return newList;

and then you need to catch it as well:

by that I mean instead of this:

 eliminateDuplicates(list);

you need this:

 list = eliminateDuplicates(list);
-2

public static void sortList(int[] list)

while(numOfNums != -1 && x < list.length)

eliminateDuplicates(list);

These are the 3 things I picked out from your code, mind you I picked them out pretty quickly too. We'll start from the top:

public static void sortList(int[] list)

Literally does nothing. Why? Simple! It returns nothing. For it do return SOMETHING it needs to have a return type of int[] in your case.

while(numOfNums != -1 && x < list.length)

When initializing arrays like you are, you are giving it it's array.length value inside of the brackets. A list with 0 items is worthless to your code. Heck, a list with 1 item is worthless to your code. So maybe change it to:

while(numOfNums > 1 && x < list.length)

Lastly:

eliminateDuplicates(list);

This, also does nothing. Yet again, why? Alas, yet again, it's simple. It may be returning an int[] but you do nothing with what it returns. To use it change it to:

array = eliminateDuplicates(list);

or probably better suited to your case:

newList = new int[eliminateDuplicates(list).length]; newList = eliminateDuplicates(list);

This will set it to the exact size of what the list will be, and after that it just sets it equal to the array that is returned.

11
  • 1
    The sortList method does do something, because the modifications to the array are destructive.
    – durron597
    Commented Apr 17, 2015 at 18:08
  • 2
    @Nicholas for your sake you should understand what having a 'void' function means. It doesn't 'do nothing' stackoverflow.com/questions/2390063/… Commented Apr 17, 2015 at 18:11
  • @matrixanomaly in his case it does. He's using local variables therefore he isn't editing anything he'll actually use. Commented Apr 17, 2015 at 18:30
  • @durron597 When I say nothing, I mean it doesn't help him in any way. It obviously does something if it has lines of code in it. Commented Apr 17, 2015 at 18:32
  • lol no. Just because he doesn't return anything doesn't mean it does nothing. He is shifting things around, which whether helpful or not, is executing a procedure. Many people including me thought you literally meant it 'does nothing' (hence downvotes). You might want to edit your question to say that "method isn't doing what you think it is ... etc". It is doing something. not nothing. Method there is trying to sort a list. Commented Apr 17, 2015 at 18:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.