11 Replies Latest reply: Jan 15, 2013 9:58 AM by abillconsl RSS

    Please help me

    984565
      I have been working on a set of three files for my APCompSci elective(I should have taken art...) https://www.dropbox.com/sh/d3sxugds5vpxzm0/ccnbuy0bG1 (I will also print them at the bottom of this message)
      you will note that there are 4 files here:
      IndexMaker:DO NOT EDIT THIS FILE, RUN IT WHEN YOU THINK IT ALL THE CODE IS READY
      IndexEntry: please help me with this file, there are comments inside it to help you understand it better
      DocumentIndex: please help me with this file, there are comments inside it to help you understand it better
      fish.txt: this is the file that is supposed to be indexed.
      When run properly, the output should be a new .txt titled"fishIndex" which should list all the words in fish.txt alphabetically, and all the lines they appear on.

      my guess is that screw ups are toString() and addWord()
      Ignore foundOrInserted()-it has been commented out.

      Thank You!

      /*
      * This program takes a text file, creates an index (by line numbers)
      * for all the words in the file and writes the index
      * into the output file. The program takes input and output file names
      * from the command-line args or prompts the user for the file names.
      */

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

      public class IndexMaker
      {
      public static void main(String[] args) throws IOException
      {
      Scanner keyboard = new Scanner(System.in);
      String fileName;

      // Open input file:

      if (args.length > 0)
      fileName = args[0];
      else
      {
      System.out.print("\nEnter input file name: ");
      fileName = keyboard.nextLine().trim();
      }

      BufferedReader inputFile =
      new BufferedReader(new FileReader(fileName), 1024);

      // Create output file:

      if (args.length > 1)
      fileName = args[1];
      else
      {
      System.out.print("\nEnter output file name: ");
      fileName = keyboard.nextLine().trim();
      }

      PrintWriter outputFile =
      new PrintWriter(new FileWriter(fileName));

      // Create index:

      DocumentIndex index = new DocumentIndex();

      String line;
      int lineNum = 0;
      while ((line = inputFile.readLine()) != null)
      {
      lineNum++;
      index.addAllWords(line, lineNum);
      }

      // Save index:

      for (IndexEntry entry : index)
      outputFile.println(entry);

      // Finish:

      inputFile.close();
      outputFile.close();

      System.out.println("Done.");
      }
      }



      import java.util.ArrayList;

      public class DocumentIndex extends ArrayList<IndexEntry>
      {
      /*
      * Creates an empty DocumentIndex with the default
      * initial capacity.
      */
      public DocumentIndex()
      {
      super();

      }

      /*
      * Creates an empty DocumentIndex with a given
      * initial capacity.
      */
      public DocumentIndex(int initialCapacity)
      {
      super(initialCapacity);

      }

      /*
      * If word is not yet in this DocumentIndex,
      * creates a new IndexEntry for word, and inserts
      * it into this list in alphabetical order;
      * adds num to this word's IndexEntry by calling
      * its add(num) method.
      */
      public void addWord(String word, int num)
      {
      Integer nummy = new Integer (num);
      if(!(contains(word))){// i would put this whole thing first
      int a=0;
      while(a<size()){
      if(get(a).getWord().compareToIgnoreCase(word)<1){
      a++;
      }
      else {
      add(a,get(a));
      break;
      }
      }
      }
      for(int i=0; i<size();i++){ //do you mean size?
      if(get(i).getWord().equalsIgnoreCase(word)){//equals w/ or w/o case?
      add(nummy, get(i));
      }
      }

      return;




      }

      /*
      * For each word in str, calls addWord(word, num).
      */
      public void addAllWords(String str, int num)
      {
      String [] words;
      words = str.split("\\W+");
      for (int i=0;i<words.length;i++){//again, are you sure abour length?
      if (!(words.equals(""))){
      addWord(words[i],num);
      }
      }
      }


      /*
      * Tries to find an IndexEntry with a given word in this
      * DocumentIndex. If not found, inserts a new IndexEntry for
      * word at the appropriate place (in alphabetical order).
      *
      * Returns the index of the found or inserted IndexEntry
      */
      /*private int foundOrInserted(String word)
      {
      int i;
      while(i<size()){
      if(get(i).getWord().compareToIgnoreCase(word)<0){
      // your code here
      get(i).add(i-1,word);
      return i-1;
      }
      if (i==size()-1){
      add(word);

      i++;
      }

      * loop through the array list and compare word
      * to the i'th word in the ArrayList.
      *
      * If word is the same as the i'th word in the ArrayList
      * just return i.
      *
      * otherwise if this word is alphabetically before the i'th
      * word of the ArrayList, then create a new IndexEntry
      * containing word and add it to the ArrayList at the
      * i'th position, and then return i
      */
      }


      import java.util.ArrayList;

      public class IndexEntry
      {
      // define some fields here

      private ArrayList<Integer> numsList;//numbers are line numbers where "word" occurs in input file
      private String word;
      /*
      * Constructs an IndexEntry for a given word
      * (converted to upper case); sets numsList
      * to an empty ArrayList.
      */
      public IndexEntry(String w)
      {
      word=w.toUpperCase();
      numsList= new ArrayList<Integer>();
      }

      /*
      * Returns the word of this IndexEntry object.
      */
      public String getWord()
      {
      return word;
      }

      /*
      * If num is not already in the list, adds num
      * at the end of this IndexEntry's list
      * of numbers.
      */
      public void add(int num)
      {
      Integer ans = new Integer (num);
      if(numsList.contains(ans)){
      return;
      }
      else{
      numsList.add(ans);
      return;
      }


      }



      /*
      * Converts this IndexEntry into a string in the
      * following format: the word followed by a space, followed by
      * numbers separated by a comma and a space.
      */
      public String toString()
      {
      String ans="";
      for (int i=0; i<numsList.size(); i++){//nums list.length?
      ans+=numsList.get(i)+", ";
      }
      return ans;
      // your code here
      }
      }
        • 1. Re: Please help me
          EJP
          What's the question?
          • 2. Re: Please help me
            984565
            The question is: Can you please make this work properly?
            • 3. Re: Please help me
              EJP
              What's wrong with it?
              • 4. Re: Please help me
                gimbal2
                981562 wrote:
                The question is: Can you please make this work properly?
                the answer to that is yes, but the problem is that you have to do it so it isn't going to help you that other people can do it.
                • 5. Re: Please help me
                  984565
                  When I run the program, it appears to work, but when I open the new .txt file, it is blank. It is n longer about the grade, the time for that has past. I just want to get this thing working, but I can't figure out which of the methods is screwy. My guess is that it is either the addWord() method or toString() method.
                  • 6. Re: Please help me
                    984565
                    It is no longer a question of getting a good grade, the time for that has past. I just want to see the program run. I think the problem is in the addWord() method or in the toString() method, because when I run the IndexMaker.class, the newly made file is blank.
                    • 7. Re: Please help me
                      aksarben
                      Advice:

                      1. Use a meaningful subject line. A vague "Help Me" almost guarantees that many people will not bother to read the post.

                      2. Use the proper tags to format the code. Very few people will read unformatted code (and I'm not one of them).
                      • 8. Re: Please help me
                        abillconsl
                        Well a quick look shows the method addWord takes a String and you are passing in an array of Strings. This won't compile.
                        • 9. Re: Please help me
                          abillconsl
                          Your problem seems to be in addWord, yes. You should play with println or assert statements to see what you got. For example, it seems that your initial size() is 0.
                          • 10. Re: Please help me
                            EJP
                            aksarben wrote:
                            Advice:

                            1. Use a meaningful subject line. A vague "Help Me" almost guarantees that many people will not bother to read the post.

                            2. Use the proper tags to format the code. Very few people will read unformatted code (and I'm not one of them).
                            3. Ask a real question. I had to ask twice to get anything of the sort out of you.
                            4. Provide any error messages, exceptions,etc.
                            • 11. Re: Please help me
                              abillconsl
                              No reply? You need a very small change to IndexEntry.java. And a number of changes to DocumentIndex.java. If you also make a small change to IndexMaker.java you will have fewer changes to make in DocumentIndex.java. If you want to work through them let me know.