-2

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: enter image description here

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!

  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Progman Nov 14 '21 at 19:12
  • Sorry sadly no, I have declared a int variable = 26, which is used to define the size of other vectors and matrices. So the problem is not just getting out of the bounds, but the use of ascii here: int c = pattern.charAt(j) - 'a'; That is line of code was created only for lowercases, when you give it uppercases it crashes the vectors and/or matrices, since the ascii value of an uppercase is different from an uppercase –  Nov 14 '21 at 19:20
  • 2
    Please [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. – Progman Nov 14 '21 at 19:21

1 Answers1

1

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;
    
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34