file access (Sequential search)

L

lei

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

Lee Weiner

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
 
D

Daniel Pitts

Lee said:
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.
 
L

lei

Lee said:
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..
 
L

lei

Lee said:
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..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top