Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Program error (http://www.go4expert.com/forums/program-error-t3810/)

Zahid 9Apr2007 20:00

Program error
 
Hi,

I want to develop a program in java such that it should be capable of handling the given input and writing it into the files and then it should fetch the written data to count the no of chars, words, spaces, special symbols etc. Upto now i've written the program as shown below. I could not able to rectify the errors. Could u pls help me in ascertaining the same and explain me why it has occured and what would be its remedy?


Java Code:
Code:

import java.io.*;

public class FileRW{
  public static void main(String[] args) throws IOException{
    FileOutputStream fos = new FileOutputStream("file1.txt");
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String stored;
    int characters=0, spaces=0, words=0, lines=0, specialChars=0;

    System.out.println("Enter the string\n");
    System.out.flush();
    stored = br.readLine();
   
    for(int i=0;i<stored.length();i++){
      fos.write(stored.charAt(i));
    }
    fos.close();
   
    FileOutputStream fis = new FileOutputStream("file1.txt");
    FileInputStream fin = new FileInputStream("file1.txt");
    int nofbytes = (int)fis.available();
    byte inBuff[] = new byte[nofbytes];
    String readed = (String)new String(inBuff);
    String readed1 = fis.readLine();
   
    for(int i=0;i<readed1.length();i++){
     
        char c= readed1.charAt(i);
     
   
        if(c>='a' && c<='z'){
          characters++;
        }
        if(c==' '){
          spaces++;
          words++;
        }
        if(c=='\n'){
          lines++;
        }
    }
    fis.close();
    fin.close();
   
    System.out.println("Number of characters:"+characters+"\nNumber of spaces:"+spaces+"\nNumber of Words:"+words+"\nNumber of lines:"+lines);

  }
}



All times are GMT +5.5. The time now is 05:47.