# Word duplicate finder and counter

import java.util.Collections;//for frequency of words
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
/**
Output--In  phrase count duplicate substrings or strings or with space
psuedo code---
1.splt phrase by particular length
get list of each particular splited length word
for each word in list
for each word count if its count> 1 after having counted at least one, remove duplicates from List
getword and its frequency
2.split phrase with space then get frequency of words
3.splt phrase by particular length space included
then get frequency of words
*/
public class DuplicateSubString {
//@SuppressWarnings("unchecked")
public static void main(String[] args) {

// String phrase = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";
//String phrase ="How do you do in How";
String phrase ="How do you do in Howdy How";

Scanner sc = new Scanner( System.in );
System.out.println( "Please enter how many characters of grouping between 0 to 5" );
int splitleng=splitleng = sc.nextInt();
if(splitleng>0){
int string_to_test=1;
System.out.println( "Please enter type 1 for with space or else without space " );
string_to_test=sc.nextInt();
if(string_to_test==1){
System.out.println( "split with specified group of characters space included");
getListwithFreq(listOfWords( phrase,splitleng));
}else{
System.out.println( "split phrase with space & check  also in substring for repition");
getListwithFreq(wordRepitionList( phrase, splitleng));
}

}else{
System.out.println( "space split and get frequency of words");
getListwithFreq(wordRepitionList( phrase));
}

System.out.println("Phrase is   ---"+ phrase);
}

//phrase splited by particular length include space
private   static List<String> listOfWords(String phrase,int splitleng ){
int k=phrase.length();
ArrayList< String > wordlist = new ArrayList< String >();
for(int l=0;l<=k-splitleng;l++){
String s4=(phrase.substring(l,l+splitleng));
//System.out.println("s4["+l+"]"+s4 );
}
return wordlist;
}
//pass list of word which is splited by particular length include space
private static void getListwithFreq(List<String> items){
int count = 0;
//System.out.println("test 111");
Map<String,Integer> wordsWithCountHMap=new HashMap<String,Integer>();
for( int i = 0; i < items.size(); i++ )
{
//System.out.printf( "item: %s ", items.get( i ) );
for( int j = 0; j < items.size(); j++ )
{
//System.out.println( " items.size() "+items.size()+" items of j "+j+" is "+items.get( j ) );
if( items.get( i ).equals( items.get( j ) ) ){
count++;
//System.out.println("i "+i+" j "+j +" count "+count);
}
if( items.get( i ).equals( items.get( j ) ) && count > 1 ){
items.remove( j );                  // after having counted at least one, remove duplicates from List
//System.out.println("in both i "+i+" j "+j +" count "+count);
}
}
//System.out.printf( " count %d\n", count );
//same above frequency of words we can get by {count=Collections.frequency(items, word)}
wordsWithCountHMap.put(items.get( i ),count);
count = 0;
}
printMap(wordsWithCountHMap);
}

public static void printMap(Map<String, Integer> map){
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
}

}
private static List<String> wordRepitionList(String phrase){
//System.out.println("word list break with space ");
String[] wordlist=phrase.split(" ");
//List<String> list = Arrays.asList(wordlist); Arrays will not suport remove for list and gives unsuportedexception
List<String> list=new ArrayList<String>();
for(String word:wordlist)
//System.out.println("test ");
return list;
}
//get list of all duplicate in SubStrings and duplicates in string too
private static List<String> wordRepitionList(String phrase,int splitleng){
String[] wordlist=phrase.split(" ");
List<String> listSubStringInWord=new ArrayList<String>();
int m=0;
for(String word:wordlist){
int k=word.length();

if(k>splitleng){//subtring duplicates
for(int l=0;l<=k-splitleng;l++){
String s4=(word.substring(l,l+splitleng));
int getfromcurrentm=listSubStringInWord.size();
}

}else{
}
}
return listSubStringInWord;
}

}//class


# Spaces

I see many code segments like this:

splitleng>0
string_to_test=1
string_to_test=sc.nextInt()


Put spaces between your two expressions to result in this:

splitleng > 0
string_to_test = 1
string_to_test = sc.nextInt()


I consider the latter a lot more readable.

As a sidenote (but this is a matter of preference): I'm not a fan of adding spaces inside brackets like here:

getListwithFreq(wordRepitionList( phrase, splitleng));


That space doesn't add any readability and it is very inconsistently used in your code. I prefer this:

getListwithFreq(wordRepitionList(phrase, splitleng));


Even more about spaces: consider the following method declaration:

private   static List<String> listOfWords(String phrase,int splitleng ){


# Naming

Keep naming conventions in mind. A method should be in the form of <verb><subject> with an obvious example for a getter: getFirstName(). At first glance I can immediately say what this method will do: it will return me data and I know what data.

Will I know what it does when its name is listOfWords()? Using get- and set- prefixes are very common and you should try to use them wherever applicable.

Same goes for capitalization! In Java, we use lowerCamelCase for our methods. This means that

getListwithFreq


becomes

getListWithFreq


Aside from that: write the entire word, don't shorten it. Freq should become Frequency. Also try to make your name more descriptive: right now it seems as if your method will take 2 arguments, a list and a frequency. This is not reflected in your method's actual signature.

Another note: your class is named DuplicateSubString. This bears no meaning in itself and thus I would suggest something that actually describes what it does: DuplicateSubStringFinder.

# Encapsulation

Encapsulation exists in many forms but in the end they all have one thing in common: hide implementationd details.

You have a variable named wordsWithCountHMap. What if you decide to use a TreeMap later on? Now you'll have to change the name of this variable! That's unacceptable, programmers are supposed to be lazy. Hide the actual implementation details by simply not putting it in the name (you're not having your firstName declared as firstNameString either).

# Declaration

This code

ArrayList< String > wordlist = new ArrayList< String >();


Can be abbreviated to (notice the name- and spacingchanges)

List<String> wordList = new ArrayList<>();


You're already partially doing this here

Map<String,Integer> wordsWithCountHMap=new HashMap<String,Integer>();


so this can be turned just as well into

Map<String,Integer> wordsWithCountHMap = new HashMap<>();


Your method printMap is declared as public but should only get called from inside your current class. Make it private instead.

# Static

All your methods are declared as static so they can directly be accessed from your main method. While it seems easier, it's also useless and curses at OO programming.

Instead, make them all instance methods (aka: remove the static modifier) and create an instance of DuplicateSubString instead. Now you can work with this class and it's a lot more OO-oriented.

# Conclusion

All together you have many recurring code issues but because many of them can be categorized in the same aspects you will find it easy to pull your game to the next level if you adapt just a few changes.

Your code will become a lot easier to read and interpret by adding some spacing and choosing clearer names.

• Please let me know for anymore improvements @jeroen-vannevel – user2628716 Apr 7 '14 at 3:00
• @user2628716: please rollback your post and create a new one with the new situation. – Jeroen Vannevel Apr 7 '14 at 18:57
• how to rollback – user2628716 Apr 13 '14 at 14:34
• It's okay, a moderator has already done it for you. If you have a new question, simply create a new post. – Jeroen Vannevel Apr 13 '14 at 14:37