I am trying 2 do this question:
Write a program that reads a set of words and looks up a dictionary to find if each word is there. Your program should use the following set of words, read from a file that you have created carefully with any text editor so there are no mis-spellings:
Java class interface
Pascal Gosling Wirth
data structure polymorphism
Kernighan Ritchie Aho
object method field
antidisestablishmentatrian
ism
For the dictionary I have a text file that contains around 24000 words, that this dictionary is conveniently arranged in ASCII order (upper case precedes lowercase).
The program should produce output arranged as follows:
Words found in dictionary:
<a list of words, one per line>
Words not found in dictionary:
<a list of words, one per line>
Design requirements and hints:
Use one of the binary search methods from the textbook that searches for a Comparable object from an ordered array of Comparable objects.
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
The following program is the first part of the whole program, there are some errors. Can anyone correct for me?
//import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
public class Dictionary
{
//Read an unlimited number of String; return a String []
public static String [] getString()
{
FileReader fr = null;
fr = new FileReader(checkWords); // checkWords is my own created txt file, but cannot resolve symbol)
String [] array = new String[25000];
String oneLine;
int itemsRead = 0;
try
{
while((oneLine = fr.readLine()) != null && !oneLine.equals("")) //(method for readLine cannot resolves symbol)
{
if(itemsRead == array.length)
array = resize(array,array.length * 2);
array[itemsRead++] = oneLine;
}
}
catch(IOException e)
{
System.out.println("");
}
return resize(array,itemsRead);
}
//Resize a String [] array; return new array
public static String [] resize(String [] array, int newSize)
{
String [] original = array;
int numToCopy = Math.min(original.length, newSize);
array = new String[newSize];
for(int i=0; i <numToCopy; i++)
array[i] = original[i];
return array;
}
public static void main(String [] args)
{
String [] array = getString();
for(int i=0; i < array.length; i++)
System.out.println(array[i
]);
}
/**
* Performs the standard binary search
* using two comparisons per level.
* @return index where item is found, or NOT_FOUND.
*/
public static int binarySearch(Comparable [] a, Comparable x)
{
int low = 0;
int high = a.length - 1;
int mid;
while(low <= high)
{
mid = (low + high) / 2;
if(a[mid].compareTo(x) < 0)
low = mid+1;
else if(a[mid].compareTo(x) > 0)
high = mid - 1;
else
return mid;
}
return NOT_FOUND; //(cannot resolved symbol)
}
}