Reply
Mon 15 Jun, 2009 02:20 pm
/i want to count the no of alphabets from a to z in a file. The file is "2.txt" and its contents are as under:
prateek
rawal
manit
/
import java.io.*;
public class count
{
public static void main(String args[])
{
int i=0;
String s;
try
{
BufferedReader b2= new BufferedReader(new FileReader("2.txt"));
while((s=b2.readLine())!=null)
{
System.out.println(s);
for(int j=0;j<s.length();j++)
{
if(s.charAt(j)>=97 && s.charAt(j)<=122)
System.out.println(i+=s.length());
}
}
b2.close();
}
catch(IOException o)
{
o.printStackTrace();
}
System.out.println("The no of alphabets in the file are"+i);
}
}
/There is no compilation error but it gives wrong output....please tell me what is the problem./
@Prateekr10,
If I understand the purpose of your code correctly, shouldn't this line increment i by one instead of by the length of the string?
Quote:System.out.println(i+=s.length());