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