1

I'm creating a TicTacToe game and I've come across an issue with my strings

Code:

import java.util.Scanner;

public class TicTacToe {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String player1Letter;
        String player2Letter;

        System.out.print("Player 1 Name: ");
        String player1 = input.next();
        System.out.print('\f');

        System.out.print("Player 2 Name: ");
        String player2 = input.next();
        System.out.print('\f');

        System.out.print(player1 + ", Choose X or O: ");
        String letter = input.next();

        if (letter.equalsIgnoreCase("x")) {
            player1Letter = "X";
        } else {
            player2Letter = "O";
        }

        if (letter.equalsIgnoreCase("o")) {
            player1Letter = "O";
        } else {
            player2Letter = "X";
        }

        System.out.print('\f');
        System.out.println("How To");
        System.out.println("-------");
        System.out.println();
        System.out.println(" 1 | 2 | 3 ");
        System.out.println("-----------");
        System.out.println(" 4 | 5 | 6 ");
        System.out.println("-----------");
        System.out.println(" 7 | 8 | 9 ");
        System.out.println();

        while (true) {
            System.out.print("Type 'begin' To Begin: ");
            String begin = input.next();
            if (begin.equalsIgnoreCase("begin")) {
                break;
            } else if (!begin.equals("begin")) {
                System.out.print('\f');
                System.out.println("Incorrect Syntax");
                System.out.println();
            }
        }
        System.out.println(player1 + "'s Turn " + player1Letter);
        System.out.println("-----------------------------------");
        System.out.println();
        System.out.println("   |   |   ");
        System.out.println("-----------");
        System.out.println("   |   |   ");
        System.out.println("-----------");
        System.out.println("   |   |   ");
    }
}

Towards the bottom where it says

System.out.println(player1 + "'s Turn " + player1Letter);

I get the error saying "variable player1Letter might not have been initialized". I created the strings outside of the if-statements and initialized them inside of the if-statement. Now I'm calling it, I can't figure out what's wrong here. Thank you!

Trevn Jones
  • 341
  • 2
  • 4
  • 11

5 Answers5

3

change your initialization code like this:

if (letter.equalsIgnoreCase("x")) {
    player1Letter = "X";
    player2Letter = "O";
} else {
    player1Letter = "O";
    player2Letter = "X";
}

so you will initialize both variables always

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • This will work, though it is worth noting, for example, that if "z" or "lampshade" is entered, then the latter case is executed even though the prompt asks for X or O. – Monkeygrinder Nov 04 '15 at 00:03
2

This message is telling you that there is at least one path through the code where the variable is left uninitialized before its first use. This definite assignment analysis eliminates one of most frequent causes of errors in other languages.

In this case, if letter equals neither "x" nor "o", then player1Letter will not be initialized.

You can fix this by ensuring that player1Letter and player2Letter are always initialized before their first access. One way to do this is:

boolean isPlayer1X = letter.equalsIgnoreCase("x");
String player1Letter = isPlayer1X ? "X" : "O";
String player2Letter = isPlayer1X ? "O" : "X";
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
2

You're initializing a string on one side of an if but not on the other. You need to initialize a string on both sides of the same if for the compiler to be able to be certain the string has been initialized.

Simplest option would be to initialize both strings at the time of declaration.

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
0

Try this instead:

player2Letter = "O";
player1Letter = "X";
if (letter.equalsIgnoreCase("x")) {
    player2Letter = "X";
    player1Letter = "O";
}
Blue Boy
  • 620
  • 1
  • 6
  • 13
0

you are initializing the string inside an if body. Read the error carefully. The if statement might not be true all the times. so to make sure that your variable is initialized always, initialize it outside the if condition. Hope this helps :)

Sameer
  • 26
  • 4