ArrayList as Archive

Viewed 150

My task is: The program should read items from the user. When all the items from the user have been read, the program prints the information of each item.

For each item, its identifier and name should be read. If the identifier or name is empty, the program stops asking for input, and prints all the item information. Modify the program so that after entering the items, each item is printed at most once. Two items should be considered the same if their identifiers are the same (there can be variation in their names in different countries, for instance).

If the user enters the same item multiple times, the print uses the item that was added first. I have done the code below, but it will still add items with the same identifier.

Why? How can I fix it?

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        ArrayList<String> itemsName = new ArrayList();
        ArrayList<String> itemsIdentifier = new ArrayList();

        while(true){
            System.out.println("Identifier? (emppty will stop)");
            String identifier = scanner.nextLine();
            if( identifier.isEmpty()){
                break;
            }
            System.out.println("Name? (emppty will stop");
            String name = scanner.nextLine();
             if(name.isEmpty()){
                break;
            }
            if(!(itemsIdentifier.contains(identifier))){
                itemsIdentifier.add(identifier);
            }
            if(!(itemsName.contains(name))){
                itemsName.add(name);
            }
        }

        System.out.println("==Items==");
        int j = 0;
        for(String i: itemsIdentifier){
            System.out.println(i + ": "+ itemsName.get(j));
            j++;
        }

    }
}
4 Answers

I think the problem with your code is that you are adding name into itemsName list even when you are not adding identifier into itemsIdentifier list in the following lines

if(!(itemsIdentifier.contains(identifier))){
    itemsIdentifier.add(identifier);
}
if(!(itemsName.contains(name))){
    itemsName.add(name);
}

Ideally shouldn't you either add both name and identifier or don't add any?

You have a while(true) loop which will keep going indefinitely, you are breaking the loop only if the user input is empty, but you are not doing anything when the lists already contain an identifier more than once. (You are adding to the list if you have a unique identifier).

EDIT I have other misgivings on the code above (I am assuming that this is an assignment), but since I do not know if you built this on top of what the lecturer gave you, I can only speculate. Some thoughts on the code above:

  1. You could create your own Record object.
  2. As per the instructions, you would need to override the equals method.
  3. Instead of having two lists, you would have only 1 list of type Record.

Try something like this:

if(!(itemsIdentifier.contains(identifier))) {
    itemsIdentifier.add(identifier);
    itemsName.add(name);
}

In your code, if the id is already in the list, the name could still be added...

You only need to check whether the identifier is similar or not. The name similarity condition is not required. As long as the identifiers are different( though they have the same name), you still add them to the itemsIdentifier and itemsName. On the other hand, if the identifiers are identical, there is no need to run a check on the similarity of the name.

Related