I need a way to do the same operation with much less code. This will help me to understand Java in much better way. OUTPUT of the below code will be: new, boy, 3pm, to
public class substring {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str= "a new, boy with new haircut boy, 3pm to boy 8pm margian 3pm to ghost";
String concant = "";
int occurance =0;
str = str.replaceAll(",", "");
System.out.println(str);
String[] subStr = str.split("\\s");
for(String sub:subStr)
{
for (String sub1:subStr) {
if(sub.equals(sub1))
{
occurance++;
if(occurance>=2)
{
if(!concant.contains(sub))
{
if(concant!= "")
concant = concant +", "+ sub;
else
concant = sub;
}
}
}
}
occurance = 0;
}
System.out.println(concant);
}
}