Home > OS >  Why, when I in method index1=path.indexOf(jdk); I pass a string variable to jdk, the compiler throws
Why, when I in method index1=path.indexOf(jdk); I pass a string variable to jdk, the compiler throws

Time:06-20

Why, when I in method index1=path.indexOf(jdk); I pass a string variable to jdk, the compiler throws exceptions, and when I put "jdk" in quotes everything works fine(index1=path.indexOf("jdk");) . This variable already has a string type, why quote it, this variable already refers to a string type object

public class Solution {
    public static void main(String[] args) {
        String path = "/usr/java/jdk1.8/bin/";

        String jdk13 = "jdk-13";
        System.out.println(changePath(path, jdk13));
    }


    public static String changePath(String path, String jdk) {
        //System.out.println(jdk);
        int index1=path.indexOf(jdk);
        int index2=path.indexOf("/",index1);
        String oldjdk=path.substring(index1,index2);

        return path.replace(oldjdk,jdk);
    }
}

CodePudding user response:

It's substring(index1,index2) that's throwing a IndexOutOfBoundsException (a StringIndexOutOfBoundsException, to be precise).

index1 is negative, because the string "jdk-13" is not contained in path (while path does contains the string "jdk" you try to pass explicitly when it works), so indexOf returns -1 (because it didn't find the substring) and substring doesn't expect a negative number as its first parameter.

In other words, read the documentation for indexOf and substring. All those problems are explained there.

As an aside, this is not an error you're getting from the compiler, it's a runtime exception. You'd better learn the difference sooner rather than later.

CodePudding user response:

I tried out your code as posted. It compiles fine (no compile-time errors), but it did throw this when I ran it:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin -1, end 0, length 21

That error came from this line, clearly index has a value of "-1":

String oldjdk=path.substring(index1,index2);

Let's confirm that by adding this:

System.out.println("index1: "   index1);

Sure enough, it is "-1":

index1: -1

Why did it end up as "-1"? The value index1 is set from this line: int index1=path.indexOf(jdk) – what are the other values, for path and jdk? Let's print them out, too:

int index1 = path.indexOf(jdk);
System.out.println("path: "   path);
System.out.println("jdk: "   jdk);
System.out.println("index1: "   index1);

Here's the new output, which shows clearly that the value for jdk – that is, "jdk-13" – does not appear as a substring anywhere within the value for path. If we look at the Javadoc for substring() we see that the method returns -1 if no value is found in the string.

path: /usr/java/jdk1.8/bin/
jdk: jdk-13
index1: -1

Your original questions:

why does it throw an error when you use the variable jdk?

It throws an error because you're trying to replace a substring which doesn't exist, but your code isn't checking for that (remember, it does give you a "-1" return value, you could check that before proceeding in your code).

As a small addition, you could add a guard before calling substring(), this wouldn't fix the program entirely, but it would prevent it from blowing up with an exception:

if (index1 >= 0) {
    oldjdk = path.substring(index1, index2);
    return path.replace(oldjdk, jdk);
} else {
    System.out.println("index1 is a non-positive number: "   index1);
    return path; // since modification didn't work, return the original path
}

why does it work correctly when you use a hard-coded value of "jdk"?

It works correctly in that case because you're using a hard-coded value – "jdk" – which does exist as a substring in the value of path. That is, "/usr/java/jdk1.8/bin/" contains a substring "jdk", so the code that looks for index (int index1=path.indexOf(jdk)) will return a value that works with the rest of the code.

  • Related