How do I fix my duplicate detection code? Java

Anon

I have here a scanner in which collects series of numbers. I want it to scan the list every time user inputs a number so if the user inputs a number that is already in the list the new input will be disregarded/ignored and at the same time not adding increment to the loop.

The problem is the code can't seem to identify the duplicates. It continues to register the duplicate number even after few tries.

My code so far:

public class Number {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How Many Numbers You want to Enter:");
        int n = input.nextInt();
        
        List<Integer> number = new ArrayList<>();
        
        for(int s=0;s<n;s++) {
            int t = input.nextInt();
            for (int j = 1; j < number.size(); j++) {
                if (t == number.get(j)) {
                    System.out.print("Duplicate!");
                    s--;
                    continue;
                } else {
                    number.add(t);
                }
            }            
        }
    }
}
sorifiend

At the moment nothing at all is being saved in the number list, so the first thing to do is add debugging to work out why, or better yet, we can make use of the ArrayList.contains(...) method to solve this quite easily without needing the nested loop that that is causing your issue, for example the following works:

for(int s=0;s<n;s++) {
    int t = input.nextInt();
    if(number.contains(t)){
        System.out.print("Duplicate!\r\n");
        s--;
        continue;
    } else {
        number.add(t);
    }          
}
//Print the result
System.out.println(Arrays.deepToString(number.toArray()));

And an output for a length of 5 and this number sequence 2,3,7,3,5,1 is:

How Many Numbers You want to Enter:5
2
3
7
3
Duplicate!
5
1
[2, 3, 7, 5, 1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

How do I fix my FirebaseRecyclerAdapter on a fragment?

Inputting a string into my code that an int is required and I get a java error how can I fix this?

My code for this newton's cradle isn't working. How do I fix this?

How do I fix my JLabel not appearing in the intended JPanel? (Code utilizes JLayeredPane)

Why is this code not working, How do i fix it?

How do I fix the indentation error in my code?

How do I fix git duplicate folder names remotely only

How Do I Fix My Wait And Repeat Code? I Cant Click The Button That Is Repeating an action

How do I fix my code for plotting a histogram?

How can I fix my code to avoid returning duplicate pairs while using map in racket?

how do I fix my code in order to display one table?

How do I fix my neomuttrc syntax?

How do I fix my Java code so that it ends when a user types "quit"?

How do I fix Webpack from mangling my code in production?

How do I fix plotting my boxplot?

How do i fix my code's vulnerability?

How do I fix an issue with my HTML code?. The screen displays text I haven't written

I am getting errors in my code and i don't know how to fix(Java fx stackPane)

How do i fix my class code 'int' object error?

How do I fix my code about a script that stopped working?

How do I fix my code with Pygame to jump correctly?

How do I fix my code so that when the button is pressed it will only increment the vote count of the corresponding post?

How do i fix it in Java

How do I fix my code so that it is automated?

how do i fix this subprocess error in my code

How can I fix my java Stack<T> code where I got nullpointerexception?

How do I fix my code for Python Mad lips loops?

I am struggling with a for loop for an array. How do I fix my java code?