forum

Home / DeveloperSection / Forums / Search for a StringBuffer object in an ArrayList?

Search for a StringBuffer object in an ArrayList?

Anonymous User 1712 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?

Updated on 22-Dec-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By