Please ignore the bad file names but this is how I have done it so far. I want to count all ASCII
characters in a file in Java
but it is getting an "Array out of bounds error" with large text
This code:
class CreateZipFile {
public static void main(String[] args) {
try {
CharacterCounter();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getClass().getSimpleName() + "-" + e.getMessage());//Throws nice output message
}
}
private static void CharacterCounter() throws IOException{
FileInputStream fstream = new FileInputStream("/Users/Devonte1/Desktop/Javatest.txt");//Read in file
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));//Take file stream and place it into bufferedReader
OutputStreamWriter bw = null;
String strLine="";
String removeSpace="";
while ((strLine = br.readLine()) != null) {
removeSpace+=strLine;
}
String st=removeSpace.replaceAll(" ", "");//Replace all spaces
char[]text = st.toCharArray();//Create new conjoined character array
System.out.println("Character Total");
int [] count = new int [256];//Character array
//Create index
for(int x = 0; x < 256; x ++){
count[x]=0;
}
//Search file
for (int index = 0; index < text.length; index ++) {
char ch = text[index];
int y = ch;
count[y]++;
}
//
for(int x = 0; x < 256; x++){
char ch= (char) x;
if (count[x] == 0){
System.out.println("Character not used"+ " "+ ch + " = (char code " + (int) ch + ")");
}
else if (count[x] != 0){
System.out.println("Character " + ch + " used" + count[x] + " = (char code " + (int) ch + ")");
}
}
}
}
Error:
Error:Arrayoutofboundexception: 8217
What am i doing wrong?