Program error

Discussion in 'Java' started by Zahid, Apr 9, 2007.

  1. Zahid

    Zahid New Member

    Joined:
    Apr 6, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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);
    
      }
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice