This discussion is archived
11 Replies Latest reply: Jan 15, 2013 7:58 AM by abillconsl RSS

Please help me

984565 Newbie
Currently Being Moderated
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
}
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points