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!

[2921 byte] By [croqueta] at [2007-11-27 2:39:26]
# 1
use the .equal() method, == operator only checks to see if two objects references are the same and not the values contained within
MaxxDmga at 2007-7-12 3:01:27 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you for your suggestion MaxxDmg, but I finally solved it by using the string.indexOf() method.

I leave the code just in case someone may have a problem simmilar to mine.

Thank you again for answering.

import java.io.*;

import java.lang.*;

public class filter {

public static void 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

FileReader words = new FileReader ("words.txt");

BufferedReader in = new BufferedReader(words);

String line;

int pos;

try {

//While it doesn't get to EOF show only the words beggining with the letter inputted

while ((line = in.readLine()) != null) {

if((pos=line.indexOf(letter)) == 0)

System.out.println(line);

}

}catch(Exception e){

System.out.println("No words beginning with letter: " + letter);

}

//Close buffers

words.close();

in.close();

}

}

croqueta at 2007-7-12 3:01:27 > top of Java-index,Java Essentials,New To Java...