Question

I have a question about writing a code that can check if what the user inputs (a string) is a duplicate of a string that an array already stored:

First of all I want to point out I checked with the question on here about duplicates but they are either answered with importing packages other than Scanner or checking duplicates in an already int or string stored arrays. This is different:

        int numberOfPigs = Integer.parseInt( keyboard.nextLine() );
        Pigs pigArray = new pigs[numberOfPigs];

        for (int i = 0; i < pigArray.length; i++){

            System.out.println("Pig name: " + (i+1));
            String name = keyboard.nextLine();
            String [] tempArray = new String [numberOfPigs];

            tempArray[i]=name;

            for (int k = i+1; k < pigArray.length; k++){

            while (tempArray[i].equals( tempArray[k])){
                    System.out.println("The ID is duplicate.");
                    tempArrays[i] = keyboard.nextLine();
                }
                System.out.println("Not a duplicate! Yay.");
            }

...code not over. the for loop with int i still has to go through several inputs from user before looping back to the beginning and go through i++ with taking in the next pig name. The rest part of the code I have no problem with, it's just that whenever i run this code, the duplicates are still allowed in... Any tips are appreciated. Thank you.

Was it helpful?

Solution

You made 2 mistakes in your code.

  1. You were redeclaring your tempArray in your initial for loop.
  2. You were counting up in your k loop when you should have been counting down.

    int numberOfPigs = Integer.parseInt(keyboard.nextLine());
    Pigs pigArray = new pigs[numberOfPigs];
    String[] tempArray = new String[numberOfPigs]; // I moved this here see
                                                        // comment below
    for (int i = 0; i < pigArray.length; i++) {
    System.out.println("Pig name: " + (i + 1));
    String name = keyboard.nextLine();
    // first problem was you were redeclaring your tempArray here
    // thereby erasing previous elements over every iteration of i
    tempArray[i] = name;
    for (int k = i - 1; k >= 0; k--) { // you need to count backwards
                            //you only check what was already entered for duplicates
                             // you were doing the opposite
    while (tempArray[i].equals(tempArray[k])) {
        System.out.println("The ID is duplicate.");
        tempArray[i] = keyboard.nextLine();
    }
    System.out.println(" Not a duplicate! Yay.");
    }// k loop
    }// i loop
    

OTHER TIPS

int numberOfPigs = Integer.parseInt( keyboard.nextLine() );
Pigs pigArray = new pigs[numberOfPigs];

for (int i = 0; i < pigArray.length; i++){

   System.out.println("Pig name: " + (i+1));
   String name = keyboard.nextLine();


   String [] tempArray = new String [numberOfPigs];

tempArray is now an array with numberOfPigs, where each element is null.

   tempArray[i]=name;

Now the first element in tempArray is the user-input name, but all other elements are still null.

   for (int k = i+1; k < pigArray.length; k++){

Why are you using pigArray.length, yet you are not using pigArray in the loop at all?

      while (tempArray[i].equals( tempArray[k])){

In fact, you're comparing the first element in the array, which is not null (it's the user input), against all the other elements, which are null! Of course it won't think anything is a duplicate, because you're checking the it against a whole bunch of nothing.

Since the user-input name is never equal to null, that is being falsely interpreted as "not a duplicate", and allowed through.

         System.out.println("The ID is duplicate.");
         tempArrays[i] = keyboard.nextLine();
      }
   System.out.println("Not a duplicate! Yay.");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top