0
   

IO question

 
 
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./
 
  2  
Reply Mon 15 Jun, 2009 02:24 pm
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());

0 Replies
 
 

Related Topics

The "Death" of the Computer Mouse - Discussion by Phoenix32890
Blue screen of Death - Question by dagmaraka
How does one uninstall a program? - Question by Woollcott
how do you type pi? - Question by C99
Conficker worm - Discussion by msolga
SYSTRAY MISSING - Discussion by Misti26
Broad Jump Client Foundation - Discussion by Mustang
 
  1. able2know
  2. » IO question
Copyright © 2009 Horizontal Verticals :: Page generated in 0.34 seconds on 11/23/2009 at 12:55:56 Top End