Trevn Jones
Trevn Jones

Reputation: 333

String Initializing

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!

Upvotes: 1

Views: 99

Answers (5)

Sameer
Sameer

Reputation: 26

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 :)

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86411

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";

Upvotes: 2

Darwin von Corax
Darwin von Corax

Reputation: 5246

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.

Upvotes: 2

Blue Boy
Blue Boy

Reputation: 610

Try this instead:

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

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24146

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

Upvotes: 3

Related Questions