February 03, 2012

Count Characters AtoZ and Special Characters From File:

public class CountCharactersFromFile {
public static void main(String[] args) throws Exception {
FileInputStream fstream = new FileInputStream(
"C:\\Myjavacafe\\data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
String strLine = "";
String str = "";
while ((strLine = br.readLine()) != null) {
str += strLine;
}
while ((strLine = br.readLine()) != null) {
str += strLine;
boolean ifFlag = false;
while ((str = br.readLine()) != null) {
String[] st = str.split(" ");
System.out.println("st.toString():"+st.toString());
System.out.println("strLine: "+strLine);
if(strLine.equals(st.toString())){
ifFlag = true;
System.out.println("Flag: "+ifFlag);
}
}
}
String st = str.replaceAll(" ", "");
char[] third = st.toCharArray();
System.out.println("Character Total");
for (int counter = 0; counter < third.length; counter++) {
char ch = third[counter];
int count = 0;
for (int i = 0; i < third.length; i++) {
if (ch == third[i])
count++;
}
boolean flag = false;
for (int j = counter - 1; j >= 0; j--) {
if (ch == third[j])
flag = true;
}
if (!flag) {
System.out.println(ch + " " + count);
}
}
}
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts