user3324728
user3324728

Reputation: 1

Why does this line of code not work?

index = temp.indexOf(" \"");

This line of code is supposed to put index in the position of " but instead index = -1

temp = /path/to/image.jpg “Title text”)

Here is the whole method index = line.indexOf("![");

            if (index > -1)
            {
                working = line.substring(0,index);
                String temp = line.substring(index+2,line.length());
                index = temp.indexOf("](");
                String altText = temp.substring(0, index);

                System.out.println(temp);

                temp = temp.substring(index+2, temp.length());

                System.out.println(temp);

                index = temp.indexOf(" \"");

                System.out.println(index);

                String imgPath = temp.substring(0, index);
                temp = temp.substring(index+2, line.length());
                index = temp.indexOf("\")");
                String titleText = temp.substring(0,index);
                temp = temp.substring(index+2, line.length());
                working = working + translateImage(altText, imgPath, titleText);
                working = working + temp;
                line = working;
                working = "";
            }

This is the error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
alternate text](/path/to/image.jpg “Title text”)
/path/to/image.jpg “Title text”)
-1
    at MarkdownTranslator.main(MarkdownTranslator.java:105)

I was using the strings after the exception for checking the values of the following variables respectively. temp temp index

Upvotes: 0

Views: 86

Answers (1)

AlphaGeek
AlphaGeek

Reputation: 392

If the second line of code is a Copy&Pasted version of the string, then it looks like the string actually contains "smart quotes". These are not equivalent to the '"' character.

See the following:

Upvotes: 2

Related Questions