1

Im making a small school project, keep in mind i'm a beginner. Im gonna make a small system that adds member numbers of members at a gym to an array. I need to make sure that people cant get the same member number, in other words make sure the same value doesnt appear on serveral index spots.

So far my method looks like this:

public void members(int mNr){
    if(arraySize < memberNr.length){
        throw new IllegalArgumentException("There are no more spots available");
    } 
    if(memberNr.equals(mNr)){
        throw new IllegalArgumentException("The member is already in the system");
    }
    else{
        memberNr[count++] = mNr;
    } 
}

While having a contructor and some attributes like this:

int[] memberNr; 
int arraySize;
int count; 

public TrainingList(int arraySize){
    this.arraySize = arraySize;
    this.memberNr = new int[arraySize];
}

As you can see i tried using equals, which doesnt seem to work.. But honestly i have no idea how to make each value unique I hope some of you can help me out Thanks alot

1
  • 1
    An array will never be "equals" to an int. You need to test if the array contains the int. Commented Nov 23, 2019 at 15:35

3 Answers 3

3

You can use set in java

Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.

mport java.util.*; 
public class Set_example 
{ 
    public static void main(String[] args) 
    { 
        // Set deonstration using HashSet 
        Set<String> hash_Set = new HashSet<String>(); 
        hash_Set.add("a"); 
        hash_Set.add("b"); 
        hash_Set.add("a"); 
        hash_Set.add("c"); 
        hash_Set.add("d"); 
        System.out.print("Set output without the duplicates"); 

        System.out.println(hash_Set); 

        // Set deonstration using TreeSet 
        System.out.print("Sorted Set after passing into TreeSet"); 
        Set<String> tree_Set = new TreeSet<String>(hash_Set); 
        System.out.println(tree_Set); 
    } 
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Im not allowed to use sets, it has to be a regular array unfortunately
for (int i = 0; i < memberNr.length; i++) { for (int j = i + 1 ; j < memberNr.length; j++) { if (memberNr[i].equals(memberNr[j])) { // got the duplicate element } } }
2
public void members(int mNr){
    if(arraySize < memberNr.length){
        throw new IllegalArgumentException("There are no more spots available");
    } 
    //You need to loop through your array and throw exception if the incoming value mNr already present
    for(int i=0; i<memberNr.length; i++){
       if(memberNr[i] == mNr){
         throw new IllegalArgumentException("The member is already in the system");
        }
     }
    //Otherwise just add it
     memberNr[count++] = mNr;
}

I hope the comments added inline explains the code. Let me know how this goes.

3 Comments

Thanks alot, easy to understand and it works perfectly - might have learned something new today!
Good to know that. Cheers and great day ahead.
You can also use ArrayUtils.contains or the Stream API as described here if you want to get rid of the for-loop.
0

Hey you can’t directly comparing arrays (collection of values with one integer value) First iterate the element in membernr and check with the integer value

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.