Users Pricing

forum

home / developersection / forums / search for a stringbuffer object in an arraylist?

Search for a StringBuffer object in an ArrayList?

Anonymous User 1990 22 Dec 2015
Following is the code snippet I am working with.

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
while (N-- > 0) {
   str = new StringBuffer(sc.next());
   if (al.contains(str)) {
       System.out.println("Duplicate value " + str);
   } else {
       al.add(str);
   }    
}
If the input is: 4

abc
fgh
dfg
abc

It is showing blank output when the expected output is:

Duplicate value abc

Where am I going wrong here?

I am a content writter !


1 Answers

Anonymous User 22 Dec 2015 Accepted Answer