Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Java counting the number of times a character appears in a file

New2AllThis:

I am aware that this question may have been asked before, but after browsing through the other questions I couldn't find a solution for my problem. So I am trying to count the number of times a certain Character Appears in a .txt file. My code is able to count the number of times a character appears, but it is not ignoring the case of the letter so when it should output 15 'a' instead it does 13 'a' because there are two capital A's in my .txt file.

Here is my code:


import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Program6 {
ArrayList countOfLetters;
int count(String filename, char ch)
{
System.out.println(ch);
File fin = new File(filename);
Scanner scan = null;
try{

}
catch(Exception e)
{

}
return 0;
}
void writeCountResult ( String filename )
{
File fin = new File(filename);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Program6 program6 = new Program6();
String filename = "xanadu.txt";
program6.countOfLetters = new ArrayList();
for(int i=65;i {
int num = program6.count(filename,(char)i);
program6.countOfLetters.add(num);
}
program6.writeCountResult("output.txt");

//This code counts the number of times "a" appears in the file but not "A" BufferedReader reader = new BufferedReader(new FileReader("xanadu.txt"));
int ch;
char charToSearch='a';
int counter=0;
while((ch=reader.read()) != -1) {
if(charToSearch == (char)ch) {
counter++;
}
};
reader.close();

System.out.println(counter);
}

}

Part 2: Now write another method to write the counts of any three of the above letters in a file called "output.txt". Use the following method header:void writeCountResult ( String filename ). As you can see this is already in my code, but I was not sure where to go next to count the characters to an output.txt from the other txt file.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Java counting the number of times a character appears in a file

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×