Filtering words from a file
Hello everybody:
First of all thanks for your help, is not the first time that I see other users that have had similar Java issues like mine and thanks to their solutions mine have been solved too.
However this time I haven't found anything in the forum. I'm trying to read a bunch of words from a file (words.txt) which looks like this:
apple
animation
application
argument
bolts
bounce
class
....
and then show only the words beginning with a letter that has been inputted from the keyboard.
The code is this:
import java.io.*;
publicclass filter{
publicstaticvoid main(String[] args)throws IOException{
//Ask the user for the letter he wants to start the words with
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Letter: ");
String letter=br.readLine();
System.out.print("\n");
//Where to read the words from
BufferedReader in =new BufferedReader(new FileReader ("words.txt"));
String line;
line = in.readLine();
try{
//While it doesn't get to EOF show only the words beggining with the letter inputted
while (line !=null){
if(in.readLine() == letter)
System.out.println(line);
}
}catch(Exception e){
System.out.println("No words beginning with letter: " + letter);
}
//Close buffers
in.close();
out.close();
}
}
I presume that the trouble is in the lineif(in.readLine() == letter) because if for exampleletter == b the program checks the words.txt list and since the first word begins withapple it gives a false and quits checking the rest of the words.
But despite I have been messing around with it many days I haven't found a solution.
Thank you very much in advance!

