file access (Sequential search)

Discussion in 'Java' started by lei, Dec 29, 2006.

  1. lei

    lei Guest

    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>
     
    lei, Dec 29, 2006
    #1
    1. Advertising

  2. lei

    EJP Guest

    lei wrote:

    > This program is correct in syntax but semantically wrong.


    Looks OK to me. Semantically wrong *how*?
     
    EJP, Dec 29, 2006
    #2
    1. Advertising

  3. lei

    lei Guest

    EJP wrote:
    > lei wrote:
    >
    > > 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.
     
    lei, Dec 29, 2006
    #3
  4. lei

    Lee Weiner Guest

    In article <>, "lei" <> wrote:
    >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
     
    Lee Weiner, Dec 29, 2006
    #4
  5. lei

    Daniel Pitts Guest

    Lee Weiner wrote:
    > In article <>, "lei" <> wrote:
    > >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


    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.
     
    Daniel Pitts, Dec 29, 2006
    #5
  6. lei

    lei Guest

    Lee Weiner wrote:
    > In article <>, "lei" <> wrote:
    > >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


    thanks a lot! it works now..
     
    lei, Dec 29, 2006
    #6
  7. lei

    lei Guest

    Lee Weiner wrote:
    > In article <>, "lei" <> wrote:
    > >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


    thanks a lot! it works now..
     
    lei, Dec 29, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty

    Comments are welcome for two sequential search C functions

    lovecreatesbeauty, Jul 9, 2006, in forum: C Programming
    Replies:
    36
    Views:
    838
    Chris Dollin
    Jul 13, 2006
  2. lovecreatesbeauty

    Comments are welcome for two sequential search C functions

    lovecreatesbeauty, Jul 9, 2006, in forum: C Programming
    Replies:
    31
    Views:
    973
    Richard Heathfield
    Jul 11, 2006
  3. Replies:
    4
    Views:
    352
    Jerry Coffin
    Jul 28, 2006
  4. Ray Leon
    Replies:
    25
    Views:
    1,612
    John W Kennedy
    Jul 13, 2008
  5. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page