Hi I need help searching a news article. THe program is suppose to search a news articles and alphabetize all the words in the document for each article. There are 4 different articles, and its suppose to alphabetize each articles words. Right now the program just alphabetizes every word and number in the article. The text file is located below.
|
Code:
|
//This program reads an input line from the reader put the worda into an array with a count and increases
//the count each time a word is repeated. It then sorts the words alphabetically in the array and
//then prints out the array.
import java.util.StringTokenizer;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileNotFoundException;
public class WordsFrequency
{
public static void main(String[] args)
{
// Initializations
FileReader reader = null;
FileWriter writer = null;
// Open input and output files
try
{
reader = new FileReader("Reuters00.txt");
writer = new FileWriter("WordsReport.txt");
}
catch(FileNotFoundException e)
{
System.err.println("Cannot find input file");
System.exit(1);
}
catch(IOException e)
{
System.err.println("Cannot open input/output file");
System.exit(2);
}
// Set up to read a line and write a line
BufferedReader in = new BufferedReader(reader);
PrintWriter out = new PrintWriter(writer);
out.println("Copied file is: Words followed by frequency");
int count = 0;
wordCount[] wordsArray = new wordCount[100000];
boolean done = false;
while(!done)
{
String inputLine;
try
{
inputLine= in.readLine();
}
catch(IOException e)
{
System.err.println("Problem reading input, program terminates. " );
inputLine = null;
}
if (inputLine == null)
{
done = true;
sortbyWords(wordsArray,count);
for(int i = 0; i < count;i++)
out.println(wordsArray[i].toString());
}
else
{
StringTokenizer tokenizer = new StringTokenizer(inputLine);
while(tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
int lengthofString = token.length();
char ch = token.charAt(lengthofString-1);
if(ch == '.' || ch == ',' || ch == '!' || ch == '?' || ch == ';')
token = token.substring(0,lengthofString-1);
wordCount wordAndCount= new wordCount(token);
boolean skip = false;
for(int i = 0; i < count; i++)
if(token.equalsIgnoreCase(wordsArray[i].getWord()))
{
skip = true;
wordsArray[i].increaseFrequency();
}
if(skip == false)
{
wordsArray[count] = wordAndCount;
count++;
}
}
}
}
// Close files
try
{
in.close();
}
catch(IOException e)
{
System.err.println("Error closing file.");
}
finally
{
out.close();
}
}
public static void sortbyWords(wordCount [] wArray, int size)
{
wordCount temp;
for (int j = 0; j < size-1; j++)
for (int i = 0; i < size-1; i++)
{
if (wArray[i].getWord().compareToIgnoreCase(wArray[i+1].getWord()) > 0)
{
temp = wArray[i];
wArray[i] = wArray[i + 1];
wArray[i+1] = temp;
}
}
}
} |
This is the text file
<ID>23</ID>
<BODY>Brown-Forman Inc said its board
has approved a three-for-two stock split and a 35 pct increase
in the company cash dividend.
The company cited its improved earnings outlook and
continued strong cash flow as reasons for raising the dividend.
Brown-Forman said the split of its Class A and Class B
common shares would be effective March 13.
The company said directors declared a quarterly cash
dividend on each new share of both classes of 28 cts, payable
April one to holders of record March 20. Prior to the split,
the company had paid 31 cts quarterly.
Brown-Forman today reported a 37 pct increase in third
quarter profits to 21.6 mln dlrs, or 1.00 dlr a share, on a
seven pct increase in sales to a record 337 mln dlrs.
Brown-Forman said nine month profits declined a bit to 66.0
mln dlrs, or 3.07 dlrs a share, from 66.2 mln dlrs, or 3.08
dlrs a share, a year earlier due to a second quarter charge of
37 cts a share for restructuring its beverage operations.
The company said lower corporate tax rates and the
restructuring "are expected to substantially improve
Brown-Forman's earnings and cash flow in fiscal 1988."
</BODY>
<ID>249</ID>
<BODY>The International Coffee Organization
(ICO ) council talks on reintroducing export quotas continued
with an extended session lasting late into Sunday night, but
delegates said prospects for an accord between producers and
consumers were diminishing by the minute.
The special meeting, called to stop the prolonged slide in
coffee prices, was likely to adjourn sometime tonight without
agreement, delegates said.
The council is expected to agree to reconvene either within
the next six weeks or in September, they said.
The talks foundered on Sunday afternoon when it became
apparent consumers and producers could not compromise on the
formula for calculating any future quota system, delegates
said.
Coffee export quotas were suspended a year ago when prices
soared in response to a drought which cut Brazil's crop by
nearly two-thirds. Brazil is the world's largest coffee
producer and exporter.
</BODY>
<ID>19</ID>
<BODY>The Commodity Credit Corporation, CCC,
has accepted an export bonus offer to cover the sale of 37,000
long tons of wheat flour to North Yemen, the U.S. Agriculture
Department said.
The wheat four is for shipment March-May and the bonus
awarded was 119.05 dlrs per tonnes and will be paid in the form
of commodities from the CCC inventory.
The bonus was awarded to the Pillsbury Company.
The wheat flour purchases complete the Export Enhancement
Program initiative announced in April, 1986, it said.
</BODY>
<ID>47</ID>
<BODY>inflation
plan, initially hailed at home and abroad as the saviour of the
economy, is limping towards its first anniversary amid soaring
prices, widespread shortages and a foreign payments crisis.
Announced last February 28 the plan froze prices, fixed the
value of the new Cruzado currency and ended widespread
indexation of the economy in a bid to halt the country's 250
pct inflation rate.
But within a year the plan has all but collapsed.
"The situation now is worse than it was. Although there was
inflation, at least the economy worked," a leading bank
economist said.
The crumbling of the plan has been accompanied by a
dramatic reversal in the foreign trade account. In 1984 and
1985 Brazil's annual trade surpluses had been sufficient to
cover the 12 billion dlrs needed to service its 109 billion dlr
foreign debt.
For the first nine months of 1986 all seemed to be on
target for a repeat, with monthly surpluses averaging one
billion dlrs. But as exports were diverted and imports
increased to avoid further domestic shortages the trade surplus
plunged to 211 mln dlrs in October and since then has averaged
under 150 mln.
</BODY>
<ID>48</ID>
<BODY>New Zealand's official foreign
reserves fell to 7.15 billion N.Z. Dlrs in January from 7.20
billion dlrs in December and compared with 3.03 billion a year
ago period, the Reserve Bank said in its weekly statistical
bulletin.
</BODY>
<ID>58</ID>
<BODY>Assets of money market mutual funds
increased 720.4 mln dlrs in the week ended yesterday to 236.90
billion dlrs, the Investment Company Institute said.
Assets of 91 institutional funds rose 356 mln dlrs to 66.19
billion dlrs, 198 general purpose funds rose 212.5 mln dlrs to
62.94 billion dlrs and 92 broker-dealer funds rose 151.9 mln
dlrs to 107.77 billion dlrs.
</BODY>