I am using a text file to read values from and loading this file into a buffered reader. thereafter i am reading the file line by line and checking if any of line contains one of my keywords (i already have them in a list of String).
However, even though the line contains the keyword i am looking for it does not detects it and gives it a Miss. Here is the code
for(int i=0;i<sortedKeywordList.size();i++)
{
String tempString=sortedKeywordList.get(i);
while(US.readLine()!=null)
{
String str=US.readLine();
//System.out.println(str);
if(str.contains(tempString)){
System.out.println("Contains: "+tempString);
}
else{
System.out.println("Miss");
}
}
}
For each keyword, you’re iterating through your buffer using readLine(). So after your first keyword, you’ll have exhausted your buffer reading and the next keyword test won’t even execute since
US.readLine()is giving you null. You’re not re-initialising your reader.So why not iterate through your file once (using your
readLine()structure), and then for each line iterate through your keywords ?EDIT: As Hunter as pointed out (above) you’re also calling readLine() twice per loop. Once in your loop test and once to check each line for a keyword. I would first of all ensure you’re reading the file correctly (simply by printing out each line as you read it)