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 / General / December 2006

Tip: Looking for answers? Try searching our database.

file access (Sequential search)

Thread view: 
lei - 29 Dec 2006 04:09 GMT
This program is correct in syntax but semantically wrong.  Please check
it out.

<code>

import java.io.*;
import java.lang.*;

class SearchWord{
    public static void main(String args[]) throws IOException{

    InputStreamReader stdin = new InputStreamReader(System.in);
    BufferedReader console = new BufferedReader(stdin);
    System.out.print("Enter a word: ");
    String input = console.readLine();

    BufferedReader in=null;
    String word;
    int check=1;

    try{
        in = new BufferedReader(new FileReader("words.txt"));
    }

    catch(FileNotFoundException e){
        System.out.println("Error opening words.txt.");
        System.exit(1);
    }

    try{
        while((word = in.readLine())!= null){
            if(word.equalsIgnoreCase(input))
                check = 0;
                break;
        }
    }

    catch(IOException ReadError){
        System.out.println("Error reading word.txt.");
        System.exit(1);
    }

    if(check==0)
        System.out.println("The Word Exists.");
    else
        System.out.println("The Word does not exist.");

    try{
        in.close();
    }

    catch(IOException e){
        System.out.println("Error closing words.txt.");
        System.exit(1);
    }
   
    }
}

</code>
EJP - 29 Dec 2006 04:25 GMT
> This program is correct in syntax but semantically wrong.

Looks OK to me. Semantically wrong *how*?
lei - 29 Dec 2006 04:36 GMT
> > This program is correct in syntax but semantically wrong.
>
> Looks OK to me. Semantically wrong *how*?

If you type a word, the word, at all times, does not exist.
Lee Weiner - 29 Dec 2006 05:09 GMT
>This program is correct in syntax but semantically wrong.  Please check
>it out.

       try{
               while((word = in.readLine())!= null){
                       if(word.equalsIgnoreCase(input))
                               check = 0;
                               break;
               }
       }

You're missing braces around the two statements in the "if" block.  As it
stands, the break is executed on the first pass of the loop.

Lee Weiner
lee AT leeweiner DOT org
Daniel Pitts - 29 Dec 2006 05:24 GMT
> >This program is correct in syntax but semantically wrong.  Please check
> >it out.
[quoted text clipped - 12 lines]
> Lee Weiner
> lee AT leeweiner DOT org

it ALSO appears that you are testing "input" (the word to search for),
and "word" (the a whole line of the input file), for equality (ignoring
case).  If you are searching for a word, you'll need to split the read
line into words.
lei - 29 Dec 2006 05:32 GMT
> >This program is correct in syntax but semantically wrong.  Please check
> >it out.
[quoted text clipped - 12 lines]
> Lee Weiner
> lee AT leeweiner DOT org

thanks a lot! it works now..
lei - 29 Dec 2006 05:38 GMT
> >This program is correct in syntax but semantically wrong.  Please check
> >it out.
[quoted text clipped - 12 lines]
> Lee Weiner
> lee AT leeweiner DOT org

thanks a lot! it works now..


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.