0

This is my code.

public class Test {
    public void main (String[] args) {

        int a;
        String b = "11343468/32145";

        String c[] = b.split("");

        for(int i=0; i<c.length; i++) {
            if(c[i].equals("/")) {
                a = i;
            } else {a = 0;}
        }
        System.out.println(a);
    }

I'm trying to find the index of / in String[] c.

But when printing a, eclipse said The local variable a may not have been initialized.

I know I can print a if I put System.out.println(a); in if-else {}, but I have more works to do on this code so I must use printing code outside if-else.

I used else code but still have an error. What's the problem?

1
  • 3
    The compiler doesn't know that the length of the array c is guaranteed to be greater than zero. It therefore doesn't know whether for-loop will be entered. If it isn't, the variable is never initialised. Commented Apr 1, 2020 at 16:34

6 Answers 6

1

Like already mentioned in the comments, the compiler's analysis does not go far enough that it can guarantee the for-loop ever runs, and therefore the variable is not "definitely assigned" as required by the language.

To find the index of a character in a string, you don't need for loops or if statements. You can just use the indexOf method:

    String b = "11343468/32145";
    int a = b.indexOf('/');
Sign up to request clarification or add additional context in comments.

Comments

1

You need to initialize variable value while declaring in method. You can find the code below, There is different ways to do the same.Below in one of them.

public class Test {
    public static void main (String[] args) {

        int a = -1;
        String b = "11343468/32145";

        String c[] = b.split("");

        for(int i=0; i<c.length; i++) {
            if(c[i].equals("/")) {
                a = i;
                break;
            }
        }
        System.out.println(a);
    }
}

1 Comment

what if: String b = "/1345"; It will be unclear if / was found in the string, or if 0 was printed because of the initial value of b.
0

@Michael's comment is correct, if you modify your code like this, the message will go away:

public class Test {
    private static final NOT_FOUND = -1;
    public void main (String[] args) {

        int a = NOT_FOUND;
        String b = "11343468/32145";

        String c[] = b.split("");

        for(int i=0; i<c.length; i++) {
            if(c[i].equals("/")) {
                a = i;
            } else {a = 0;}
        }
        System.out.println((a == NOT_FOUND ? "NOT_FOUND" : a);
    }
}

The constant NOT_FOUND is unnecessary, but it clarifies what is happening. Keep in mind that continuing your loop after you've found "/" will likely overwrite the "found" value of a.

2 Comments

But there is no point in looping here: you may as well only check the last character.
@AndyTurner you are correct, however I wasn't intending to solve the whole problem for the OP. The OP's request was for assistance in addressing the error message from the compiler. Notice my closing sentence in my post.
0

I assume a is printing as 0 in your case, even if you initialize the variable a . its because using else you are always making it 0 at the end . please use something like this:

    int a = 0;
    String b = "11343468/32145";
    String c[] = b.split("");
    for (int i = 0; i < c.length; i++) {
        if (c[i].equals("/")) {
            a = i;
            break;
        } else {
            a = 0;
        }
    }
    System.out.println("the value is" + a);

Comments

0

I will take a different approach. From code org point of view, fetching the index should be a separate operation, which would be rather a function. So your code will be

public class Test {
    public static void getIndex (String str, String match) {
        String c[] = str.split("");
        for(int i=0; i<c.length; i++) {
            if(c[i].equals(match)) {
                return i;
            }
        } 
    }
}

In case you want last index, then

public class Test {

    public static void getIndex (String str) {
        int index = -1;
        String c[] = str.split("");
        for(int i=0; i<c.length; i++) {
            if(c[i].equals(match)) {
                index = i;
            }
        }
        return index; 
    }
}

And then use the same in main

public static void main (String[] args) {
    String b = "11343468/32145";
    System.out.println(getIndex(b, "/"));
}

Also if you are using java, then you have two functions available in String class - String::indexOf and String::lastIndexOf. If you don't want ot use that, you have an option to use String::toCharArray(). Using String::split for this purpose is highly discouraged!

Hope this helps.

Comments

-1

For you to be able to print a it must be initialised. In your code the complier doesn't know that the for loop will be executed.

One quick way to solve this is to set a to null by using the following:

Integer a = null; // note we are using the object not the primitive int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.