Kind of ackward:
- you open the file and you scan it for all "A"
- then you scan it for all "B"
you have to "rewind" "close/open" the file befor that because you will be at EndOfFile
if the file has 20,000 lines good luck
better to read the file only once an have an array of count
and use a Scanner a lot easier
CODE
import java.io.File;
import java.util.*;
public class BruteForce
{
public static void main(String[] args )
{
char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
Scanner scan;
try {
scan = new Scanner(new File("BruteForce.java"));
} catch (Exception e) {
System.out.println("File not found");
return;
}
int[] count = new int[26];
while(scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println("Line read: " + line);
char[] digit = line.toCharArray();
for(int i = 0; i < digit.length; i++) {
for(int j = 0; j < 26; j++) {
if(digit[i] == capital[j] || digit[i] == small[j]) {
count[j]++;
break;
}
}
}
}
for (int i = 0; i < 26; i++)
{
System.out.print(" " + capital[i]);
System.out.println(" " + count[i]);
}
}
}