Incorrect output

Hi...basically this program gets a character from user and checks how many times the char occoured in a file...I dont get any errors but the final count is coming out to be 0, which is incorrect

==============================================================

package examples;

import java.io.*;

//THis file counts the number of times a certain letter appears if a file

public class CountFolder {

public static void main(String[] args)throws IOException {

char c;

int count=0;

try{

System.out.println("Enter the character to be counted");

BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));

c=(char) ch.read();

//System.out.println(c);

BufferedReader br=new BufferedReader(new FileReader("c:/xanadu.txt"));

do{

if((br.read())==c){

count=count+1;

}} while(br.readLine() != null);

System.out.println("The character " + c + " appeared " + count + " times in the file!");

}

finally{

}}}

[1040 byte] By [karanJa] at [2007-11-27 6:37:50]
# 1
Put in some print statements or use a debugger so you can see at which step(s) things are not what you're assuming they are.
jverda at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...
# 2
Your condition:> while(br.readLine() != null);is causing you to read and discard the rest of the line (after reading thefirst char of the line).Suggestion: you know you've reached the EOF when the int returned by read was -1.
Hippolytea at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...
# 3

do{

if((br.read())==c){// if you read a character here...

count=count+1;

}} while(br.readLine() != null);// then don't read a whole line here, you're skipping input and throwing it away without checking it.

Here's a better way to do it.

String line;

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

// loop over all the characters in line, counting the number of occurences of c

}

hunter9000a at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...
# 4
Or:int ch;while ((ch = br.read()) != -1) {//process ch}
Hippolytea at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...
# 5
thnx HippliteI initially tried the condition with -1 but was getting a type mismatch error, probably because i was using readLine() instead of read()...I just used it with read() and its working...thnx mate...u r a champion mate!
karanJa at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...
# 6
You're welcome. Keep hackin'!
Hippolytea at 2007-7-12 18:06:16 > top of Java-index,Java Essentials,New To Java...