0

In here how can I search name by using contains? I want to match my input result of arrayList by using .contains.

This Program I create a class name DataType for using custom type data type.

    arrayList.add(new DataType(name,Integer.parseInt(age)));


    System.out.println("Search Nme : ");
    String name=new Scanner(System.in).nextLine();
    if (arrayList.contains(new DataType(name))) {
        System.out.println("Find");
    }

Or,

    System.out.println("Search Nme : ");
    String name=new Scanner(System.in).nextLine();
    System.out.println("Search age: ");
    String age=new Scanner(System.in).nextLine();

    if (arrayList.contains(new DataType(name,Integer.parseInt(age)))) {
        System.out.println("Find");
    }

Custome Data Type Class

static class DataType {

String Name;
int Age;

public DataType(String name,int age){
    Name=name;
    Age=age;
}


 public DataType(String name){
    Name=name;
}  

}

2
  • you havent specified what language you use, use tags for that Commented Sep 20, 2017 at 21:38
  • it is a java program Commented Sep 20, 2017 at 21:54

2 Answers 2

1

Overriding equals method in java is a good point as JavaProgrammer12 said.

Here is an simple example for this case.

static class DataType {

    String Name;
    int Age;

    public DataType(String name, int age) {
        Name = name;
        Age = age;
    }

    public DataType(String name) {
        Name = name;
    }

    @Override
    public boolean equals(Object fromObj) {
        DataType dt = (DataType)fromObj;

        if(Name == null) return false;
        if(dt.Age == Age) return true;
        if(dt.Name.equals(Name)) return true;

        return false;
    }
}

And you can test with the class in a main. For example,

static void containsName1(List<DataType> dtArrayLst, Scanner sc) {
    System.out.print("Search Nme1 : ");

    String name = sc.nextLine();
    if (dtArrayLst.contains(new DataType(name))) {
        System.out.println("Find");
    }

    System.out.println();
}

static void containsName2(List<DataType> dtArrayLst, Scanner sc) {
    System.out.print("Search Nme2 : ");

    String name = sc.nextLine();

    System.out.print("Search age: ");
    String age = sc.nextLine();

    if (dtArrayLst.contains(new DataType(name, Integer.parseInt(age)))) {
        System.out.println("Find");
    }
    System.out.println();
}

public static void main(String[] args) {
    String name = "tommybee";
    String age = "100";

    List<DataType> dtArrayLst = new ArrayList<DataType>();

    dtArrayLst.add(new DataType(name, Integer.parseInt(age)));

    Scanner sc = new Scanner(System.in);

    containsName1(dtArrayLst, sc);
    containsName2(dtArrayLst, sc);

    sc.close();
}

There is a good reference site if you want to learn about overriding equals method.

https://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/

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

Comments

1

You should be able to do this by overriding equals method in your custom class. As good practice override hashcode method also when overriding equals method.

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.