Initialized variables in loops not detected

I have this piece of code

    String p4name;
    String p5name;
    if(p1score > p2score && p1score > p3score){
        if(p2score > p3score){
            p1.setWinner(true);
            p2.setWinner(true);
            p3.setWinner(false);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p1.getName() + " and " + p2.getName() + " proceed to 2-Player phase **** ");
            p4name = p1.getName();
            p5name = p2.getName();
        }
        else{
            p1.setWinner(true);
            p2.setWinner(false);
            p3.setWinner(true);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p1.getName() + " and " + p3.getName() + " proceed to 2-Player phase **** ");
            p4name = p1.getName();
            p5name = p3.getName();
        }
    }
    else if(p2score > p1score && p2score > p3score){
        if(p1score > p3score){
            p1.setWinner(true);
            p2.setWinner(true);
            p3.setWinner(false);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p1.getName() + " and " + p2.getName() + " proceed to 2-Player phase **** ");
            p4name = p1.getName();
            p5name = p2.getName();
        }
        else{
            p1.setWinner(false);
            p2.setWinner(true);
            p3.setWinner(true);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p2.getName() + " and " + p3.getName() + " proceed to 2-Player phase **** ");
            p4name = p2.getName();
            p5name = p3.getName();
        }
    }
    else if (p3score > p1score && p3score > p2score){
        if(p1score > p2score){
            p1.setWinner(true);
            p2.setWinner(false);
            p3.setWinner(true);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p1.getName() + " and " + p3.getName() + " proceed to 2-Player phase **** ");
            p4name = p1.getName();
            p5name = p3.getName();
        }
        else{
            p1.setWinner(false);
            p2.setWinner(true);
            p3.setWinner(true);
            System.out.println();
            System.out.println();
            System.out.println("**** " + p2.getName() + " and " + p3.getName() + " proceed to 2-Player phase **** ");
            p4name = p2.getName();
            p5name = p3.getName();
        }
    }
    else {
        System.out.println();
        System.out.println();
        System.out.println("**** It's a tie **** ");
    }

and I attempted to create new objects using the p4name, p5name variable

    Player p4 = new Player(p4name);
    Player p5 = new Player(p5name);

but the cmd said that p4name, p5name is not initialized. Is it because it was initialized in the if/else? Is there any possible ways to overcome this without having to continue the rest of my code in the loop?

Answer

I would suggest to extract the following method:

public void setWinners(.. p1, boolean p1Win, .. p2, boolean p2Win, .. p3, boolean p3Win){
                p1.setWinner(p1Win);
                p2.setWinner(p2Win);
                p3.setWinner(p3Win);
                System.out.println();
                System.out.println();
                System.out.println("**** " + p1.getName() + " and " + p2.getName() + " proceed to 2-Player phase **** ");
}

Typically, one should not pass a boolean as a parameter of a method but here IMO it is acceptable:

    String p4name;
    String p5name;
    if(p1score > p2score && p1score > p3score){
        p4name = p1.getName();
        if(p2score > p3score){
            setWinners(p1, true, p2, true, p3, false);
            p5name = p2.getName();
        }
        else{ 
            setWinners(p1, true, p2, false, p3, true);
            p5name = p3.getName();
        }
    }
    else if(p2score > p1score && p2score > p3score){
        if(p1score > p3score){
            setWinners(p1, true, p2, true, p3, false);
            p4name = p1.getName();
        }
        else{
            setWinners(p1, false, p2, true, p3, true);
            p4name = p2.getName();
        }
        p5name = p2.getName();
    }
    else if (p3score > p1score && p3score > p2score){
        if(p1score > p2score){
            setWinners(p1, true, p2, false, p3, true);
            p4name = p1.getName();
        }
        else{
            setWinners(p1, false, p2, true, p3, true);
            p4name = p2.getName();
        }
        p5name = p3.getName();
    }
    else {
        System.out.println();
        System.out.println();
        System.out.println("**** It's a tie **** ");
        return; // Exist the game
    }

        

Leave a Reply

Your email address will not be published. Required fields are marked *