Reading words from a text file into an array
Okay, so I have the method:
publicvoid addWord(String word)
{
}
in this spellchecker file which I'm supposed to code up. This method is called by another file which adds words from the text file into the spellchecker, so the words from the text file are automatically fed into the spellchecker as soon as the program starts up. I want to store these words into an array. I can manage to get the words into an array, however, the problem arises that it stores every single word from the text file into every element of the array. I want each element of the array to store a single word from the text file so later I can write a method which sorts these words into alphabetical order. Any advice? Thanks in advance.
Many thanks for offering your help, but I managed to fix the problem myself after realising that it was all just a silly mistake. Now I'm faced with another problem which comes in the form of nullpointerexceptions.
public class SpellChecker
{
String[] words = new String[45];
String temp;
int i;
public void addWord(String word)
{
addingWord(word);
quickSort(words, 0, words.length);
for(int k = 0; k < words.length; k++)
{
System.out.println(words[k]);
}
}
public void addingWord(String word)
{
words[i] = word;
i++;
}
public void quickSort(String words[], int lo0, int hi0)
{
int low = lo0;
int high = hi0;
String middle;
if (hi0 > lo0)
{
middle = words[(lo0 + hi0) / 2];
while (low <= high)
{
while ((low < hi0) && (words[low].compareTo(middle) < 0))
++low;
while ((high > lo0) && (words[high].compareTo(middle) > 0))
--high;
if (low <= high)
{
String temp = words[high];
words[high] = words[low];
words[low] = temp;
++low;
--high;
}
}
if (lo0 < high)
quickSort(words, lo0, high);
if (low < hi0)
quickSort(words, low, hi0);
}
}
}
I sat down with it for several hours but I still can't seem to rectify the problem. Is anyone able to spot the problem? They occur at the line:
while ((low < hi0) && (words[low].compareTo(middle) < 0))
Thanks!