Question:
Need help with Java Code I keep getting this error code and I don't know why, please help. Here is my code (error shown in picture)?
George
2015-10-07 21:36:44 UTC
import java.util.*;
import java.io.*;

public class Lab01
{
File inFile;

static int [] frequency = new int [256];

public Lab01()
{
for (int i=0; i < 256; i++)
frequency[i] = 0;
}

public void FileReader(String file) throws IOException
{
inFile = new File(file);
BufferedReader reader = new BufferedReader(new FileReader(inFile));
String sline = null;
while ((sline=reader.readLine()) != null)
{
int lengthOfLine = sline.length();

for (int i=0; i < lengthOfLine; i++)
{
char a = sline.charAt(i);
int index = (int)a;
frequency[index]++;
}

}
reader.close();

}

public static void main (String[] args)
{
System.out.println("Here is the Frequency of the characters from the ASCII table taken from the file:");

try
{
Lab01 bob = new Lab01();
bob.FileReader("Speech.txt");

for (int a = 48; a < 58; a++)
{
System.out.println(a-48+" : "+frequency[a]);
}
for (int b = 65; b < 91; b++ )
{
char z = (char)b;
System.out.println(z + " : "+ frequency[b]);
}
for (int c = 97; c < 123; c++)
{
char x = (char)c;
System.out.println(x + " : " + frequency[c]);
}

}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
Five answers:
Flexon
2015-10-07 22:33:32 UTC
Like already mentioned you are accessing the frequency array with an index which is out of the bounds of the array.



Set a breakpoint at line 29 and use the debugger to see what value index actually get when you get there. You will see that at some point in the execution it will be higher than 255.



The reason for this is that it encounters a character outside of the ASCII range. char variables in Java is 16-bit Unicode so they can be higher than 255.
?
2015-10-07 22:29:30 UTC
An out-of-bounds error means that you are trying to look up an invalid index for the array, like asking for the 10th letter of "asdfg".



Well, I tried running this on http://ideone.com/FQ11se without the file reading, and all the indexing seems to work fine.

So my guess is that the data you're reading in from the file uses an encoding with more than 256 characters. Or readLine() produces special characters outside of 0 to 255. You're hitting some character number that doesn't fit into the array "frequency".
ad c
2015-10-07 21:49:02 UTC
The error is "ArrayIndexOutOfBoundsException" thus your code on line 29 which is



frequency[index]++;



exceeds the index that is available on the array. Therefore the variable "index" passed as index of the array frequency is an index which exceeds the size of the frequency array. for example:



String[] stringArr = new String[5];



//assuming that stringArr has 5 elements assigned then calling stringArr[-1] or stringArr[6] will return that exception
david
2015-10-07 22:41:25 UTC
Wrap it in an if block?



int index = (int)a;

if (index < 256)

{

frequency[index]++;

}

else

{

System.out.println("Found a weird character: " + index;

}
Chris
2015-10-08 06:55:38 UTC
Try this: http://pastebin.com/AnnnG9Sz



charAt should never return anything beyond 0-255, could you show us the contents of Speech.txt?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...