Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: JAVA HELP PLEASEEEEEEEE

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA HELP PLEASEEEEEEEE

    For this assignment you are to write a program that handles text files. The assignment requires three classes (plus library classes); we've given you two - FreqStudy, the driver, and another class, WordCount.
    FreqStudy.java

    import java.util.*;
    import java.io.*;
     
    public class FreqStudy{
      public static void main(String[] args) throws IOException
      {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter file name");
        String fileName = scan.next();
        Scanner scan2 = new Scanner(System.in);
        System.out.println("enter words to search for");
        System.out.println("enter lower case, separated by spaces");
        String wordString = scan2.nextLine();
        WordFreq f = new WordFreq(fileName,wordString);
        f.readLines();
        f.reportFrequencies();
      }
    }

    WordCount class
    public class WordCount{
     
      private String word;
      private int count;
     
      public WordCount(String w){
        word = w;
        count = 0;
      }
     
      public String getWord(){
        return word;}
     
       public int getCount(){
        return count;}
     
      public void incCount(){count++;}
     
      public String toString() {
        return(word +  " --- " + count);
      }
     
      public boolean equals(Object other){
        WordCount i = (WordCount)other;
        return (this.word.equals(i.word));
      }
    }


    As demonstrated in the example run above, an external text file name is entered first. Then any number of individual words are entered, all lower case, and separated by spaces. After doing its analysis, the program then displays a chart that gives the frequencies in the text of the indicated words, to four decimal places. Thus 2.42% of all of the words in the Heart of Darkness text file are "I", 1.33% are "he", and so forth. Pretty clearly Heart of Darkness is narrated in the first person, and is mostly about men.

    Further requirements and tips:

    * you MUST use an array of WordCount objects to keep track of the occurrences of the indicated words.

    * Use printf from Chapter 5 to format your output.

    * If s is a String, then this String class method call:
    s.split(" "); // this is a single space surrounded by quote marks

    returns an array of strings that consists of the tokens (separated by white space) in s. For example, given
    String s = "i he his she hers";
    String[] words = s.split(" ");


    Then words is this five element array of strings: {i, he, his, she, hers}

    At the course website we've provided some tips for solving this problem, and in addition we've provided some text files that you might want to experiment with. Thus we urge you to check the ProgramNotes link at the course website for further information about the assignment.

    In the box below, enter your WordFreq code. Be sure to comment your code. (As usual, do not add/use additional import statements)


    import java.io.IOException;
    import java.util.*;
     
    public class WordFreq extends Echo{
    private String wordString;
    private int ct = 0;
    private String total = "";
    private WordCount[]words = new WordCount[8];
    private String[]count;
    private String[]wordString2;
    private String s;
     
    public WordFreq(String f, String w)throws IOException{
    super(f);
    wordString = w;
    }
     
    public void processLine(String line){
    wordString2 = wordString.split(" ");
    s = line;
    total += s + " ";
    count = total.split(" ");
    for(int j = 0; j < count.length; j++){
    words[j] = new WordCount(count[j]);
    }
    for(int j = 0; j < wordString2.length; j++){
    if(wordString2[j].equals(words[j].getWord())){
    words[j].incCount();
    }
    }
    }
    This is the code I have this far but I know that it is wayyy offf



    CAN'TTT even begin to explain how confused I am. Someone please help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    First you need to properly format the posted code. Nested statements need to be indented 3-4 spaces.
    Not all statements should start in the first column.

    Which part of the assignment are you working on and having problems with?
    Please ask some specific questions about the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    I am working on the last code that I posted. I am trying to read in a handful of words on one line. Then every time you read in a new line form a file I want to check if any of the words you're reading in is one of those. If it is, you count it (and throw it away) otherwise you just throw it away.

    And he requirements that I posted above, Im trying to be specific but I am just so SO confused.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    What part are you having problems with?
    trying to read in a handful of words on one line.
    check if any of the words you're reading in is one of those.
    If it is, you count it
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Yes, it is supposed to use text files then count the words you enter in that file. But when I enter which words to count it doesn't work. I don't know exactly which part of my code if wrong for this though. Sorry, confusing I know.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Work on one problem at a time:
    can the program do this: read in a handful of words on one line.

    --- Update ---

    First you need to properly format the posted code. Nested statements need to be indented 3-4 spaces.
    Not all statements should start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Yes the code reads in the words that I type in the Java prompt to box.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    If the program is able to get a line of words, how about the next step:
    check if any of the words you've read in is one of those being searched for.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    how would you check that?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    How would you do it manally? Given some words on a line what steps need to be done to check if any of the words are in another list somewhere?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    you would need to scan the other document to search for the words you are looking for

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Break the process down into simple, single steps. Your suggestion has too many hidden steps.
    Give a line with several words, what needs to be done first?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    read the line of words. Then go through each word...? IDK AH

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Where are the words that are to be counted? How does the program put them there?

    I assume the words to be counted have been saved somewhere before the search is made through the text to count the occurrences of the words.

    I haven't read the assignment so I don't know what the program is supposed to do. You need to do that.
    Then ask specific questions about the steps that you are having problems with.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    i posted the assignment above. I would ask more specific questions but I am so confused I really don't even know what to ask. We are pulling from files for example (Document.txt) and the program is supposed to search those files for the words we type into the box and count how many times they occur.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    Is the program given a list of words to search for?
    How does the program get that list of words and what does it do with the list?
    Have you written the code to do that yet?

    --- Update ---

    Be sure to properly format any code you post.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    no you can search any words you want. The words can be typed into the prompt box when you run the code. The scanner is used to create this.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: JAVA HELP PLEASEEEEEEEE

    What I'm asking about is: What is done with the words to be counted?
    How are the words read into the program and where does the program save them?
    Once they are saved some place, the program can read the document to search for them.
    If you don't understand my answer, don't ignore it, ask a question.