A question about ASCII and java: I am reading files using java, all of them containing Uppercase letters, which I am supposed to convert into lowercase, in order to treat them with another algorithm, which looks for matches between sequences read from both files. This algorithm contains this kind of comparison:
for(int j = 0; j < pattern.length(); ++j)
{
//char a has a certain decimal value, so whatever char subtracted to char a
//will be an index between 0 and 25
int c = pattern.charAt(j) - 'a';
And the algorithm reading the files look like this:
Anyways, whenever I read files, I get an error with the provoked by the variable text, which is supposed to have only lowercase letters. This error: ArrayIndexOutOfBoundsException caughtjava.lang.ArrayIndexOutOfBoundsException: Index -87 out of bounds for length 26
When I use a predefined String with lowercases, everything works perfectly, the only problem comes when I read the text from a file.
Is there any other way to convert uppercases into lowercases in java?
I would be very grateful if you could point out my error.
Thank you very much in advance!
The message from the exception is java.lang.ArrayIndexOutOfBoundsException: Index -87
.
That means that the expression pattern.charAt(j) - 'a'
resulted in the value -87, which in turn means that pattern.charAt(j)
must have the value 10 which is the value of '\n' - the line separator.
It seems that your text file contains not only alphabetic characters but also at least one line separator.
The are some possible solutions:
trim()
the text that you have read from the file:
text = new String(Files.readAllBytes(path)).toLowerCase().trim();
skip everything that is not in the range 'a' to 'z' in your algorithm:
int c = pattern.charAt(j) - 'a';
if (c < 0 || c > 25) continue;