The idea is simple I don't need to add objects that are already on the list. I guess the problem is that it creates a completely new object and it doesn't find a match with other identical objects that are already on the list(I might be completely wrong).
import java.util.Scanner;
import java.util.ArrayList;
public class archive {
private String indentifier;
private String name;
public archive(String indentifier,String name) {
this.name = name;
this.indentifier = indentifier;
}
public String toString() {
return this.indentifier + ": "+this.name;
}
public static void main(String[] args) {
ArrayList<archive> arhivs = new ArrayList<>();
Scanner scan = new Scanner(System.in);
String indentifier, name;
while(true) {
System.out.println("Indentifier? (empty will stop)");
indentifier = scan.nextLine();
if(indentifier.isEmpty()) {
break;
}
System.out.println("Name?(empty will stop)");
name = scan.nextLine();
if(name.isEmpty()) {
break;
}
archive newAr = new archive(indentifier,name);
if(arhivs.contains(newAr)) {
System.out.println("Item is on the list");
}else {
arhivs.add(newAr);
}
}
System.out.println("==Items==");
for(archive arhivi:arhivs) {
System.out.println(arhivi);
}
scan.close();
}
}