Results 1 to 14 of 14
  1. #1
    rubiks is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Need some help to finish this code (finding the number of unique words ...)

    How do I find the number of unique words and the total number of words from a random text file.

    This is what I got so far...
    ------------------------------------------

    import java.io.*;
    import java.util.*;
    import java.util.Scanner;
    import java.io.File;




    class test {

    public static void main(String [] args) {

    File file = new File("...; "); // random text file


    try {
    String [] word;
    int [] numer;
    int numberOfUniqueWords;
    int i;
    String s;
    String m= " ";
    Scanner f = new Scanner(file);
    while (f.hasNextLine()) {
    String line = f.nextLine();
    word=line.split(m);


    for(i=0; i<word.length; i++) {

    s=word[i];






    System.out.println();
    }

    }
    f.close();



    }catch(Exception e) {}





    }
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Go through the file line by line, split by whitespace (similar to what you're doing, though for simplicity a space is fine). Then, I suggest using a HashMap<String, Integer> to keep track of each word's count. Basically, if the hashmap already contains the word, then you increment it's integer value. Otherwise, you add the value of 1 to it. Then, at the end, iterate through it.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    A small detail: don't split on a single space " " but split on "\\s+" instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    rubiks is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Im trying to do the code without using HashMap, and Im stuck.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Quote Originally Posted by rubiks View Post
    Im trying to do the code without using HashMap, and Im stuck.
    Why? Because you don't know how Maps work? One more reason to read its API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    rubiks is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    So its not possible to do it without HashMap?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Quote Originally Posted by rubiks View Post
    So its not possible to do it without HashMap?
    Sure it is, but you have to do a lot of nasty bookkeeping programming that is already done for you by a Map ... why reinvent the wheel? If you want to use simple arrays (because you already understand them), your code will look like Fortran ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    rubiks is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Whats Fortran ?

  9. #9
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Quote Originally Posted by rubiks View Post
    Whats Fortran ?
    A programming language invented in like (I think) 1957. We've come a long way since then :P

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Quote Originally Posted by AndrewM16921 View Post
    A programming language invented in like (I think) 1957. We've come a long way since then :P
    1954; and there wasn't any formal language theory in those days yet (Noam Chomsky); it took almost 180 man years to build that first 'formula translator' ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Ahh! My first programming language. Fortran IV. Computed gotos; arithmetic if statements. And it actually passed values by name. You could change the value of literal constants!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Quote Originally Posted by jim829 View Post
    Ahh! My first programming language. Fortran IV. Computed gotos; arithmetic if statements. And it actually passed values by name. You could change the value of literal constants!
    And don't forget the biggest horror: equivalence relations for those common blocks *shudder*

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Actually, I liked equivalence relations. I wrote a solitaire game. All the variables had a specific location in the huge equivalence array. When I wanted to save state so that someone could resume their game at a later time. I just wrote out the array. It could be read it later to resume the game. Setting them all up and debugging was somewhat of a pain.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help to finish this code (finding the number of unique words ...)

    Those were the 'lame' uses of equvalence relations; the real 'fun' (mind the quotes) starts when you define an equivalence relation between D and IA where D is a REAL*8 and IA is followed by IB, as in INTEGER*4 IA, IB; oh, the sheer fun we had ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 02-25-2013, 04:41 AM
  2. Replies: 3
    Last Post: 07-08-2012, 11:08 PM
  3. Arbitrary number of objects, each with a unique name.
    By providence in forum New To Java
    Replies: 8
    Last Post: 02-23-2011, 01:12 AM
  4. Unique personal number
    By Lidiya in forum Advanced Java
    Replies: 9
    Last Post: 12-27-2010, 12:13 PM
  5. Finding words (and more) in another file
    By Lund01 in forum Java Applets
    Replies: 5
    Last Post: 09-30-2010, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •