Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Spelling Checker Program

Thread view: 
Alan Fried - 03 Dec 2004 00:45 GMT
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();
    while(line1 != null){
        while(line2 != null){
           if(line1.equals(line2))    {           
            System.out.println(line1 + "   Spelling correct");                       
            line1 = reader1.readLine();
          
        }  
        line2 = reader2.readLine();     
         
       }          
       
   }    

 }  

}
   

Here is part of the output:

aboveboard   Spelling correct
aboveground   Spelling correct
abovementioned   Spelling correct
denizen   Spelling correct
Denmark   Spelling correct
Dennis   Spelling correct
Denny   Spelling correct
impermeable   Spelling correct
impermissible   Spelling correct

This works fine when all the words are
spelled correctly. However when there
is a spelling error it shuts down and I
understand why and the reason is that
one of the files stops being read.

I've added:

if(!line1.equals(line2))    {           
            System.out.println(line1 + "   Spelling incorrect");                       
            line1 = reader1.readLine();}

after the second while loop but even if there is one error the program
reports everything incorrect.

Does anyone have any suggestions?

Thanx in advance

Alan
Sean Ingham - 08 Dec 2004 13:33 GMT
> The following program compares 2 files
> with words. If the word in one file is present
[quoted text clipped - 12 lines]
>     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();
        
>         
>      }    
[quoted text clipped - 3 lines]
>  }
>    

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#.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.