CSpadawan
CSpadawan

Reputation: 115

Some variables might not have been initialized? Why?

Programming noob here. I'm trying to write a simple program to randomly generate a tic-tac-toe board, display the Xs and Os, and then determine the outcome of the game. My problem is that when I place the String variables in curly brackets in the if/else statements it's telling me they haven't been initialized. Namely, it says variables s11, s13, s21, s22, s31, s32, and s33 haven't been initialized. Yet s12 and s23 are initialized and the only difference I can see is that they aren't in curly brackets. Where have I erred? Thanks.

import java.util.Random;

public class Lab7 {

public static void main(String[] args) {

    int b11, b12, b13, b21, b22, b23, b31, b32, b33;
    int x, o;
    String s11, s12, s13, s21, s22, s23, s31, s32, s33;

    x = 0;
    o = 0;

    Random r = new Random();
    b11 = r.nextInt(3);
    b12 = r.nextInt(3);
    b13 = r.nextInt(3);
    b21 = r.nextInt(3);
    b22 = r.nextInt(3);
    b23 = r.nextInt(3);
    b31 = r.nextInt(3);
    b32 = r.nextInt(3);
    b33 = r.nextInt(3);

    if ((b11 == b12) && (b12 == b13))
        if (b11 == 0)
        {
            o = o+1;
            s11 = "O";
        }
        else if (b11 == 1)
        {
            x = x+1;
            s11 = "X";
        }
        else
            s11 = "";
    if ((b21 == b22) && (b22 == b23))
        if (b21 == 0)
        {
            o = o+1;
            s21 = "O";
        }
        else if (b21 == 1)
        {
            x = x+1;
            s21 = "X";
        }
        else
            s21 = "";
    if ((b31 == b32) && (b32 == b33))
        if (b31 == 0)
        {
            o = o+1;
            s31 = "O";
        }
        else if (b31 == 1)
        {
            x = x+1;
            s31 = "X";
        }
        else
            s31 = "";

    if ((b11 == b21) && (b21 == b31))
        if (b11 == 0)
            o = o+1;
        else if (b11 == 1)
            x = x+1;
    if ((b32 == b22) && (b22 == b12))
        if (b32 == 0)
        {
         o = o+1;
         s32 = "O";
        }
        else if (b32 == 1)
        {
         x = x+1;
         s32 = "X";
        }
        else
            s32 = "";
    if ((b33 == b23) && (b23 == b13))
        if (b33 == 0)
        {
         o = o+1;
         s33 = "O";
        }
        else if (b33 == 1)
        {
          x = x+1;
          s33 = "X";
        }
        else
            s33 = "";

    if ((b22 == b11) && (b22 == b33))
        if (b22 == 0)
        {
         o = o+1;
         s22 = "O";
        }
        else if (b22 == 1)
        {
         x = x+1;
         s22 = "X";
        }
        else
            s22 = "";
    if ((b13 == b22) && (b22 == b31))
        if (b13 == 0)
        {
            o = o+1;
            s13 = "O";
        }
        else if (b13 == 1)
        {
            x = x+1;
            s13 = "X";
        }
        else
            s13 = "";

    if (b12 == 0)
        s12 = "O";
    else if (b12 == 1)
        s12 = "X";
    else
        s12 = "";

    if (b23 == 0)
        s23 = "O";
    else if (b23 == 1)
        s23 = "X";
    else
        s23 = "";

    System.out.println(s11+" "+s12+" "+s13);
    System.out.println(s21+" "+s22+" "+s23);
    System.out.println(s31+" "+s32+" "+s33);
}
}

Upvotes: 0

Views: 144

Answers (7)

Joni
Joni

Reputation: 111219

With nested if statements a dangling else belongs to the inner if, not to both of then. For example, if b11 does not equal b12 nothing gets assigned to s11. The easy solution is this:

if ((b11 == b12) && (b12 == b13)) {
    if (b11 == 0)
    {
        o = o+1;
        s11 = "O";
    }
    else if (b11 == 1)
    {
        x = x+1;
        s11 = "X";
    } else {
        s11 = "";
    }
}
else {
        s11 = "";
}

Also, use braces whe the parts of an if-statement spans multiple lines.

Upvotes: 0

hasan
hasan

Reputation: 24205

The error mean that depending on the input those variables may not be initialise.

for example if ((b13 == b22) && (b22 == b31)) it could result false and s13 will not be initialised.

going with String s11 = "", s12 ="" . . . .; will solve the problem but could cause logical error that you can check later. go for it for now.

Upvotes: 0

Mauren
Mauren

Reputation: 1975

This is because your variables might not get initialized, depending on the conditionals your code takes.

Just put a null value in all of them and your code should be good to go.

But remember that using == operator to compare two String objects may fail.

Upvotes: 1

Lukas Eichler
Lukas Eichler

Reputation: 5903

The compiler recognizes that in any case s12 and s23 get assined a value. For the others he don't and they need a value before they can be used. Just write to initialize every variable.

String s11="";
String s12="";
...

Upvotes: 0

Tun Zarni Kyaw
Tun Zarni Kyaw

Reputation: 2119

Use

String s11="", s12="", s13="", s21="", s22="", s23="", s31="", s32="", s33="";

instead of

String s11, s12, s13, s21, s22, s23, s31, s32, s33;

Upvotes: 1

StormeHawke
StormeHawke

Reputation: 6207

They're declared, but not necessarily initialized. In order to eliminate that compile error, they have to be initialized in every possible path through the code. The easiest way to accomplish this is to simply set your String values to null or "" when you declare them, ie

String s11 = null;

or

String s11 = "";

Upvotes: 0

Eran
Eran

Reputation: 393771

If the conditions of the if expressions are false, the sxx variables won't be initialized. They must always be initialized. You can simply initialize them to null or to an empty String.

Upvotes: 0

Related Questions