Word counter for text files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I've made a program that works, but i want to make it reusable. This code reads in a dictionary object created in another class (that i made), as well as read in a text file to put in said dictionary object. The code then loads in another text file, compares it with the dictionary, updates its frequency values and outputs to a vector. Everything works, but as i said i'm looking to make it reusable. What i mean by this is once the values have been stored in the vector, i'd like the dictionaries frequency values to reset back to 0, ready for the next text document to be compared with the dictionary, then output that to a different vector - this process will be repeated 10-12 times.



String Counter



public class StringCounter 

private LinkedList<Integer> list = new LinkedList<>();

public StringCounter(LinkedList<Integer> list)
this.list = list;


public static void main(String args)

HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");

try

Dictionary reader = new Dictionary(dictionary);

for (String text : textFileList)
reader.fileScanner(text);


Scanner textFile = new Scanner(new File("Test4.txt"));
ArrayList<String> file = new ArrayList<String>();

while(textFile.hasNext())
file.add(textFile.next().trim().toLowerCase());


for(String word : file)
Integer dict = dictionary.get(word);
if (!dictionary.containsKey(word))
dictionary.put(word, 1);
else
dictionary.put(word, dict + 1);



textFile.close();

catch(FileNotFoundException e)
e.printStackTrace();


Vector<Integer> vec1 = new Vector<>(dictionary.values());

for (Integer count : vec1)
System.out.println(count);





Dictionary



public class Dictionary 
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

// Assigns the value of the parameter to the field of the same name
public Dictionary(HashMap<String, Integer> dictionary)
this.dictionary = dictionary;


// Gets input text file, removes white spaces and adds to dictionary object
public void fileScanner(String textFileName)
try

Scanner textFile = new Scanner(new File(textFileName));

while (textFile.hasNext())
dictionary.put(textFile.next().trim(), 0);


textFile.close();

catch (FileNotFoundException e)
e.printStackTrace();



public void printDict(HashMap<String, Integer> dictionary)
System.out.println(dictionary.keySet());








share|improve this question

















  • 1




    The Dictionary class has been included in the question
    – FeelingLikeAJabroni
    Jun 20 at 12:42
















up vote
4
down vote

favorite












I've made a program that works, but i want to make it reusable. This code reads in a dictionary object created in another class (that i made), as well as read in a text file to put in said dictionary object. The code then loads in another text file, compares it with the dictionary, updates its frequency values and outputs to a vector. Everything works, but as i said i'm looking to make it reusable. What i mean by this is once the values have been stored in the vector, i'd like the dictionaries frequency values to reset back to 0, ready for the next text document to be compared with the dictionary, then output that to a different vector - this process will be repeated 10-12 times.



String Counter



public class StringCounter 

private LinkedList<Integer> list = new LinkedList<>();

public StringCounter(LinkedList<Integer> list)
this.list = list;


public static void main(String args)

HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");

try

Dictionary reader = new Dictionary(dictionary);

for (String text : textFileList)
reader.fileScanner(text);


Scanner textFile = new Scanner(new File("Test4.txt"));
ArrayList<String> file = new ArrayList<String>();

while(textFile.hasNext())
file.add(textFile.next().trim().toLowerCase());


for(String word : file)
Integer dict = dictionary.get(word);
if (!dictionary.containsKey(word))
dictionary.put(word, 1);
else
dictionary.put(word, dict + 1);



textFile.close();

catch(FileNotFoundException e)
e.printStackTrace();


Vector<Integer> vec1 = new Vector<>(dictionary.values());

for (Integer count : vec1)
System.out.println(count);





Dictionary



public class Dictionary 
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

// Assigns the value of the parameter to the field of the same name
public Dictionary(HashMap<String, Integer> dictionary)
this.dictionary = dictionary;


// Gets input text file, removes white spaces and adds to dictionary object
public void fileScanner(String textFileName)
try

Scanner textFile = new Scanner(new File(textFileName));

while (textFile.hasNext())
dictionary.put(textFile.next().trim(), 0);


textFile.close();

catch (FileNotFoundException e)
e.printStackTrace();



public void printDict(HashMap<String, Integer> dictionary)
System.out.println(dictionary.keySet());








share|improve this question

















  • 1




    The Dictionary class has been included in the question
    – FeelingLikeAJabroni
    Jun 20 at 12:42












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I've made a program that works, but i want to make it reusable. This code reads in a dictionary object created in another class (that i made), as well as read in a text file to put in said dictionary object. The code then loads in another text file, compares it with the dictionary, updates its frequency values and outputs to a vector. Everything works, but as i said i'm looking to make it reusable. What i mean by this is once the values have been stored in the vector, i'd like the dictionaries frequency values to reset back to 0, ready for the next text document to be compared with the dictionary, then output that to a different vector - this process will be repeated 10-12 times.



String Counter



public class StringCounter 

private LinkedList<Integer> list = new LinkedList<>();

public StringCounter(LinkedList<Integer> list)
this.list = list;


public static void main(String args)

HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");

try

Dictionary reader = new Dictionary(dictionary);

for (String text : textFileList)
reader.fileScanner(text);


Scanner textFile = new Scanner(new File("Test4.txt"));
ArrayList<String> file = new ArrayList<String>();

while(textFile.hasNext())
file.add(textFile.next().trim().toLowerCase());


for(String word : file)
Integer dict = dictionary.get(word);
if (!dictionary.containsKey(word))
dictionary.put(word, 1);
else
dictionary.put(word, dict + 1);



textFile.close();

catch(FileNotFoundException e)
e.printStackTrace();


Vector<Integer> vec1 = new Vector<>(dictionary.values());

for (Integer count : vec1)
System.out.println(count);





Dictionary



public class Dictionary 
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

// Assigns the value of the parameter to the field of the same name
public Dictionary(HashMap<String, Integer> dictionary)
this.dictionary = dictionary;


// Gets input text file, removes white spaces and adds to dictionary object
public void fileScanner(String textFileName)
try

Scanner textFile = new Scanner(new File(textFileName));

while (textFile.hasNext())
dictionary.put(textFile.next().trim(), 0);


textFile.close();

catch (FileNotFoundException e)
e.printStackTrace();



public void printDict(HashMap<String, Integer> dictionary)
System.out.println(dictionary.keySet());








share|improve this question













I've made a program that works, but i want to make it reusable. This code reads in a dictionary object created in another class (that i made), as well as read in a text file to put in said dictionary object. The code then loads in another text file, compares it with the dictionary, updates its frequency values and outputs to a vector. Everything works, but as i said i'm looking to make it reusable. What i mean by this is once the values have been stored in the vector, i'd like the dictionaries frequency values to reset back to 0, ready for the next text document to be compared with the dictionary, then output that to a different vector - this process will be repeated 10-12 times.



String Counter



public class StringCounter 

private LinkedList<Integer> list = new LinkedList<>();

public StringCounter(LinkedList<Integer> list)
this.list = list;


public static void main(String args)

HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");

try

Dictionary reader = new Dictionary(dictionary);

for (String text : textFileList)
reader.fileScanner(text);


Scanner textFile = new Scanner(new File("Test4.txt"));
ArrayList<String> file = new ArrayList<String>();

while(textFile.hasNext())
file.add(textFile.next().trim().toLowerCase());


for(String word : file)
Integer dict = dictionary.get(word);
if (!dictionary.containsKey(word))
dictionary.put(word, 1);
else
dictionary.put(word, dict + 1);



textFile.close();

catch(FileNotFoundException e)
e.printStackTrace();


Vector<Integer> vec1 = new Vector<>(dictionary.values());

for (Integer count : vec1)
System.out.println(count);





Dictionary



public class Dictionary 
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> dictionary = new HashMap<String, Integer>();

// Assigns the value of the parameter to the field of the same name
public Dictionary(HashMap<String, Integer> dictionary)
this.dictionary = dictionary;


// Gets input text file, removes white spaces and adds to dictionary object
public void fileScanner(String textFileName)
try

Scanner textFile = new Scanner(new File(textFileName));

while (textFile.hasNext())
dictionary.put(textFile.next().trim(), 0);


textFile.close();

catch (FileNotFoundException e)
e.printStackTrace();



public void printDict(HashMap<String, Integer> dictionary)
System.out.println(dictionary.keySet());










share|improve this question












share|improve this question




share|improve this question








edited Jun 20 at 12:42
























asked Jun 20 at 11:17









FeelingLikeAJabroni

474




474







  • 1




    The Dictionary class has been included in the question
    – FeelingLikeAJabroni
    Jun 20 at 12:42












  • 1




    The Dictionary class has been included in the question
    – FeelingLikeAJabroni
    Jun 20 at 12:42







1




1




The Dictionary class has been included in the question
– FeelingLikeAJabroni
Jun 20 at 12:42




The Dictionary class has been included in the question
– FeelingLikeAJabroni
Jun 20 at 12:42










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Usage of interfaces



HashMap<String, Integer> dictionary = ...


Try to use interfaces on the lefthand side as a best practice: Map, List, ... as it will make you independent of hashmap, linkedhashmap,...



also try not to use vector unless really necessary (https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector)



Naming



HashMap<String, Integer> dictionary = new HashMap<String, Integer>(); 
...
Dictionary reader = new Dictionary(dictionary);


There is a class dictionary, a map with the same name and when initalizing an instance of Dictionary, then it is called "reader". This is confusing



reader.fileScanner(text)


This method name does not really indicate an action. reader.scanFile(text) would be better.



Reusing dictionary data



you could make the counting of the words a method of the dictionary where you build a new map with only a count for the words encountered in the new text file(s) and return that map.



Comments



avoid comments like



// Assigns the value of the parameter to the field of the same name


as they describe what you do in the code, which you can see by reading the code. When putting a comment then at least tell why some piece of code is implemented as such (specific issue, specific algorithm, ...)



Error handling



e.printstacktrace is by preference replaced by a logging framework (log4j2, logback, slf4j as façade, ...)
Perhaps you can make the method throw ioException and make the calling code handle this?






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196882%2fword-counter-for-text-files%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Usage of interfaces



    HashMap<String, Integer> dictionary = ...


    Try to use interfaces on the lefthand side as a best practice: Map, List, ... as it will make you independent of hashmap, linkedhashmap,...



    also try not to use vector unless really necessary (https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector)



    Naming



    HashMap<String, Integer> dictionary = new HashMap<String, Integer>(); 
    ...
    Dictionary reader = new Dictionary(dictionary);


    There is a class dictionary, a map with the same name and when initalizing an instance of Dictionary, then it is called "reader". This is confusing



    reader.fileScanner(text)


    This method name does not really indicate an action. reader.scanFile(text) would be better.



    Reusing dictionary data



    you could make the counting of the words a method of the dictionary where you build a new map with only a count for the words encountered in the new text file(s) and return that map.



    Comments



    avoid comments like



    // Assigns the value of the parameter to the field of the same name


    as they describe what you do in the code, which you can see by reading the code. When putting a comment then at least tell why some piece of code is implemented as such (specific issue, specific algorithm, ...)



    Error handling



    e.printstacktrace is by preference replaced by a logging framework (log4j2, logback, slf4j as façade, ...)
    Perhaps you can make the method throw ioException and make the calling code handle this?






    share|improve this answer

























      up vote
      3
      down vote



      accepted










      Usage of interfaces



      HashMap<String, Integer> dictionary = ...


      Try to use interfaces on the lefthand side as a best practice: Map, List, ... as it will make you independent of hashmap, linkedhashmap,...



      also try not to use vector unless really necessary (https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector)



      Naming



      HashMap<String, Integer> dictionary = new HashMap<String, Integer>(); 
      ...
      Dictionary reader = new Dictionary(dictionary);


      There is a class dictionary, a map with the same name and when initalizing an instance of Dictionary, then it is called "reader". This is confusing



      reader.fileScanner(text)


      This method name does not really indicate an action. reader.scanFile(text) would be better.



      Reusing dictionary data



      you could make the counting of the words a method of the dictionary where you build a new map with only a count for the words encountered in the new text file(s) and return that map.



      Comments



      avoid comments like



      // Assigns the value of the parameter to the field of the same name


      as they describe what you do in the code, which you can see by reading the code. When putting a comment then at least tell why some piece of code is implemented as such (specific issue, specific algorithm, ...)



      Error handling



      e.printstacktrace is by preference replaced by a logging framework (log4j2, logback, slf4j as façade, ...)
      Perhaps you can make the method throw ioException and make the calling code handle this?






      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Usage of interfaces



        HashMap<String, Integer> dictionary = ...


        Try to use interfaces on the lefthand side as a best practice: Map, List, ... as it will make you independent of hashmap, linkedhashmap,...



        also try not to use vector unless really necessary (https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector)



        Naming



        HashMap<String, Integer> dictionary = new HashMap<String, Integer>(); 
        ...
        Dictionary reader = new Dictionary(dictionary);


        There is a class dictionary, a map with the same name and when initalizing an instance of Dictionary, then it is called "reader". This is confusing



        reader.fileScanner(text)


        This method name does not really indicate an action. reader.scanFile(text) would be better.



        Reusing dictionary data



        you could make the counting of the words a method of the dictionary where you build a new map with only a count for the words encountered in the new text file(s) and return that map.



        Comments



        avoid comments like



        // Assigns the value of the parameter to the field of the same name


        as they describe what you do in the code, which you can see by reading the code. When putting a comment then at least tell why some piece of code is implemented as such (specific issue, specific algorithm, ...)



        Error handling



        e.printstacktrace is by preference replaced by a logging framework (log4j2, logback, slf4j as façade, ...)
        Perhaps you can make the method throw ioException and make the calling code handle this?






        share|improve this answer













        Usage of interfaces



        HashMap<String, Integer> dictionary = ...


        Try to use interfaces on the lefthand side as a best practice: Map, List, ... as it will make you independent of hashmap, linkedhashmap,...



        also try not to use vector unless really necessary (https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector)



        Naming



        HashMap<String, Integer> dictionary = new HashMap<String, Integer>(); 
        ...
        Dictionary reader = new Dictionary(dictionary);


        There is a class dictionary, a map with the same name and when initalizing an instance of Dictionary, then it is called "reader". This is confusing



        reader.fileScanner(text)


        This method name does not really indicate an action. reader.scanFile(text) would be better.



        Reusing dictionary data



        you could make the counting of the words a method of the dictionary where you build a new map with only a count for the words encountered in the new text file(s) and return that map.



        Comments



        avoid comments like



        // Assigns the value of the parameter to the field of the same name


        as they describe what you do in the code, which you can see by reading the code. When putting a comment then at least tell why some piece of code is implemented as such (specific issue, specific algorithm, ...)



        Error handling



        e.printstacktrace is by preference replaced by a logging framework (log4j2, logback, slf4j as façade, ...)
        Perhaps you can make the method throw ioException and make the calling code handle this?







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 20 at 13:32









        Manuel

        1311




        1311






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196882%2fword-counter-for-text-files%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation