mercredi 1 avril 2020

local variable initialization problem in if-else (Java)

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?

Aucun commentaire:

Enregistrer un commentaire