Java! have to count symbols in a file -


i did code getting 0 result. can me issue? in advance

note: white-blank or space shouldn't considered symbol.

import java.util.*; import java.io.*;    public class kerkosimbolet{      private file filein;          public kerkosimbolet(file filein){         this.filein = filein;     }      public void countsymbols()throws ioexception{         int count = 0;         filereader reader = new filereader(filein);             filewriter fw = new filewriter("donati");          try{             while(reader.read()!= -1){                 //if(!reader.equals(" "))                     count++;             }         }         catch(ioexception e){             e.printstacktrace();         }         finally{             reader.close();         }         try{             fw.write(count);             fw.flush();         }         catch(ioexception e){             e.printstacktrace();         }         finally{             fw.close();         }     }      public static void main(string []arg){          file fi = new file("ubt.txt");         kerkosimbolet ks = new kerkosimbolet(fi);         try{             ks.countsymbols();         }         catch(ioexception e){             e.printstacktrace();         }     } } 

sometimes use albanian language since homework there.

heres new code came counts spaces

import java.util.*; import java.io.*;  public class kerkosimbolet{  private file filein;  public kerkosimbolet(file filein){     this.filein = filein; }  public void countsymbols()throws ioexception{     int count = 0;     filereader reader = new filereader(filein);     filewriter fw = new filewriter("donati");      try{         while(reader.read()!= -1){             if(!reader.equals(""))                 count++;         }     }     catch(ioexception e){         e.printstacktrace();     }     finally{         reader.close();     }     try{         fw.write(integer.tostring(count));         fw.flush();     }     catch(ioexception e){         e.printstacktrace();     }     finally{         fw.close();     } }  public static void main(string []arg){      file fi = new file("ubt.txt");     kerkosimbolet ks = new kerkosimbolet(fi);     try{         ks.countsymbols();     }     catch(ioexception e){         e.printstacktrace();     } } 

}

the problem lies how write count, rather counting itself. according javadocs, filewriter#write(int):

writes single character.

this not want (i guess). correct implementation be

public void countsymbols() throws ioexception{     int count = 0;     try(filereader reader = new filereader(filein)) {         int cread;         while((cread = reader.read()) != -1){             //if(!reader.equals(" "))                 count++;         }     }     try(filewriter fw = new filewriter("donati")) {         fw.write(integer.tostring(count));     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -