JSP: Count character in text file
Hi,
I am trying to count character in my text file by using the below JSP code. Even though i am free from error in the code, but the result is not what i want. I just get 1 and 0 after counting it. For example:
I have C D E G D F F F E A A in my text file.
I want to display:
A=2
C=1
D=2
E=2
F=3
And after i added some other characters into my text file the the value of the charater will increase following the whatever character that i add.
My code is below:
<HTML>
<HEAD>
<TITLE>The result.....</TITLE>
</HEAD>
<BODY>
<%@ page language = "java" import="java.io.*"%>
<% try
{
int countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF = 0;
String strPath ="C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/ROOT/15870416.txt";
String line;
BufferedReader objReader = new BufferedReader(new FileReader(strPath));
line = objReader.readLine ();
for (int i=0; i<line.length(); i++)
{
if (line.charAt(i) == 'A') countA = countA + 1;
if (line.charAt(i) == 'B') countB = countB + 1;
if (line.charAt(i) == 'C') countC = countB + 1;
if (line.charAt(i) == 'D') countD = countB + 1;
if (line.charAt(i) == 'E') countE = countB + 1;
if (line.charAt(i) == 'F') countF = countB + 1;
}
out.println(countA);
out.println(countB);
out.println(countC);
out.println(countD);
out.println(countE);
out.println(countF);
} catch(IOException e){}
%>
</BODY>
</HTML>
Could you please advise??
Kimsan
|