2

Basically, my code takes input from the user and uses a method to check if the input is unique. If it’s not unique, ask the user to enter a different one.

My idea:

import java.util.Scanner;
public class testing {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        int[] input = new int[10];

        for (int i = 0; i < 10; i++) {

            while (true) {
                int n = sc.nextInt();
                for (int j = 0; j < input.length; j++) {
                    input[j] = n;
                    checkunique(input);
                    System.out.println("Choose a different one");
                }
                break;
            }
        }

        public static boolean checkunique(int[] Array) {
            for (int i = 0; i < Array.length - 1; i++) {
                for (int j = 1 + 1; j < Array.length; j++) {
                    if (Array[i] == Array[j]) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

And sorry, I can’t figure out the rest from this point on.

0

5 Answers 5

1

You could check if the entered value is the same as another in the array before you add it to the array. You can do that by having your checking method take two parameters like this:

public static boolean checkunique(int testValue, int[] array) {
    for (int i = 0; i < array.length; i++) {
        if (testValue == array[i]) {
            return false;
        }
    }
    return true;
}

So then call the checkunique method before you put the value into the array and if the the method returns true, then add the value to the array, otherwise, ask the user to input another value. You can do something like this for your main method:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] input = new int[10];

    for (int i = 0; i < input.length; i++) {
        int n = sc.nextInt();

        boolean loop = true;
        do {
            if (checkunique(n, input)) {
                // If true
                input[i] = n;
                loop = false;
            } else {
                // If false
                System.out.println("Choose a different one.");
                n = sc.nextInt();
            }
        } while (loop);
    }
}

Note: Because the initial array is made of zeros, entering zero will be considered a duplicate. If you want to fix that, just initialize the array values to a default value of choice.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is one way to do this. You can maintain a Set , where you store user input, Sets in java only allow unique data. So, all you have to do then is check if the user entered a number that already exists in your Set or not.

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class SOTest {

    private static Set<Integer> uniqueInput = new HashSet<>();

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {

            while (true) {
                System.out.println("Choose a number:-");
                int n = sc.nextInt();

                if (!uniqueInput.contains(n)) {
                    uniqueInput.add(n);
                    if(uniqueInput.size()==10) break;
                } else {
                    System.out.println("Duplicate input");
                    continue;
                }
            }
        }
        for(Object i:uniqueInput.toArray()) {
            System.out.print(i+" ");
        }
    }
}

Comments

0

The code you post should be fine. If you have an extreme large number of users ,you may want to use HashMap.

Comments

0

You can use Set to add the elements and check whether its unique. Everytime when you add the element in the Set using add function if the same element is added again, it will return true. So you will be able to notify the user to choose a different one.

Comments

0
import java.util.*;


class InputChecker {

   public static boolean checkInput(String[] a, String b) {
       boolean isIn = false;
       for(int x = 0; x < a.length - 1; x++) {
           if(x == 1) {
              if(a[1].equals(a[0]))
                   isIn = true;
        }

        if(a[a.length - 1].equals(a[x]))
            isIn = true;
       }
        return isIn;
}

}

public class MainChecker {

   public static void main(String[] args){
        Scanner receive = new Scanner(System.in);
        InputChecker check = new InputChecker();
        ArrayList<String> checkMe = new ArrayList<String>();
        ArrayList<String> validEntry = new ArrayList<String>();
        while(true) {
        System.out.println("Please enter some input, or end to quit: ");
        String userInput = receive.nextLine();
        if(! userInput.equals("end")) {
                checkMe.add(userInput);
                String[] k =  new String[checkMe.size()];
                k = checkMe.toArray(k);
                boolean bool = check.checkInput(k, userInput);
                if(bool) {
                    System.out.println("Please enter another input: ");
                }
                else {
                    System.out.println("Input stored!");
                    validEntry.add(userInput);
                }

        }


        }

    }

}

Here is your output:

Please enter some input, or end to quit: 
Sim
Input stored!
Please enter some input, or end to quit: 
Yo
Input stored!
Please enter some input, or end to quit: 
Sim
Please enter another input: 
Please enter some input, or end to quit: 
Yo
Please enter another input: 
Please enter some input, or end to quit: 

Explanation: In this example we simply created another class with a method that can do the checking for you. We will store the user input in an ArrayList and then we can convert that ArrayList to a String array in our main program. We can pass that array to the method that we created in the other class when we call that method in our main class, and we can check to see if the current input is already in the array that has been passed to our method. We will use a boolean value in the method that we created as a "flag" and if that "flag" evaluates to "true" we know that the user input is already in the array. We then use that flag as a conditional test in our main program, we if it is "true", we know that the user input has already been stored. If the user input is already in that array, we will ask the user for more input, and we won't store it in our main storage array. However, if the user input is "unique" we will confirm that the input has been stored and put it in our main array. That is the gist of the program, and as you can see, breaking it down into classes and methods can be useful here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.