Re: Spelling Checker Program

From: Sean Ingham (inghams2_at_cs.man.ac.uk)
Date: 12/08/04


Date: Wed, 08 Dec 2004 13:33:14 +0000

Alan Fried wrote:
> The following program compares 2 files
> with words. If the word in one file is present
> in the other file the program outputs spelling
> correct and the name of the word. Here is the code
>
> import java.io.*;
>
> class SpellingFile {
> public static void main(String[] args)
> throws FileNotFoundException, IOException{
> FileReader in1 = new FileReader("spelling.txt");
> FileReader in2 = new FileReader("words.txt");
> BufferedReader reader1 = new BufferedReader(in1);
> BufferedReader reader2 = new BufferedReader(in2);
> String line1 = reader1.readLine();
> String line2 = reader2.readLine();
     --> boolean found = false;
> while(line1 != null)
         {
             --> found=false;
             --> //reset reader2 to start of file
> while(line2 != null && !found)
                 {
> if(line1.equals(line2))
                      {
> --> found=true;
> }
> line2 = reader2.readLine();
> }
               -> if(!found)
               -> {
               -> System.out.println(line1 + " Spelling
Incorrect"); -> }
               -> else
               -> {
                -> System.out.println(line1 + " Spelling correct");
               -> }
               -> line1 = reader1.readLine();
              
>
> }
>
> }
>
> }
>

You're almost there. Instead of having your new if statement test when
line1 does not equal line2, you could set a boolean flag in your second
while loop. The flag is set to true if a match is found. Then you could
have your if statement test that flag.

You then just need to reset the flag to false before a new word is
searched for.

I also noticed that your code will only work if the input is in
alphabetical order. You need to reset reader2 to the start of the file
after each word is checked. This will also solve the problem you had
where the program stops when an incoreect word is found. (That was
happening because reader2 just gets to the end of your dictionary file
and then jumps out of the loop).

Hope thats useful.
#sean#.



Relevant Pages

  • Re: Any raw data specs for the Apple IIgs BRAM?
    ... original set of example data with the original assembly code and with ... the AppleScript, and they don't match. ... ' current value of the carry flag and then change the carry flag ... ' end of the loop ...
    (comp.sys.apple2.programmer)
  • Re: semi-timely interrupt
    ... check the flag on every RTS, LOOP, BREAK instruction. ... want to insinuate an interrupt flag check into RTS, LOOP, and BREAK ...
    (comp.programming.threads)
  • Re: Thread causes 99% CPU Usage
    ... the magic word in threading is CreateEvent(), CreateMutex, ... do it on your own yes, but use Sleepeach loop. ... make that flag global or accessible from other threads so that when you have ... use WaitForSingleObject() to wait for that thread to get out... ...
    (microsoft.public.vc.language)
  • Re: Signal-Unsafe Problem
    ... About the sig_atomic_t flag issue, the problems for my program are that ... My program is also busy running other code. ... There's no need to run a busy loop for the flag alone. ... You can either process incoming data right away, ...
    (comp.os.linux.development.apps)
  • Re: Terminating a running Loop in GUI!
    ... I am having a problem in GUI. ... handles.Userdata.abort = false; % set the flag ... getting the handles structure in the loop might not be ...
    (comp.soft-sys.matlab)