0

To practice using if else, do while, and switch statements, I was making a small text adventure game where a user would be able to input their name, gain a randomly generated profession, and be assigned a randomly generated quest. however, halfway though the second goal, the java development program I was using continually said that one of my variables "might not have been initialized".

This is what i have for the code so far:

============

import java.util.*;
public class Adventure1
{
    public static void main(String[] args)
    {
        //initialize variables
        Scanner keyboard = new Scanner(System.in);
        Scanner keyboardYN = new Scanner(System.in);
        Scanner keyboard2YN = new Scanner(System.in);

        String name = "";

        char userInput;
        char userYN;
        char user2YN;

        int dieRoll = (int) (Math.random() * 9);
        char outputType;

        char Mage;
        char Soldier;
        char Explorer;
        char howTo;

        //exeternal documation
        System.out.println("The First Adventure by K. Konieczny ");
        System.out.println();

        //player name
        do
        {
            System.out.println();
            System.out.print("What is your name: ");
            name = keyboard.nextLine();
            //prompt
            System.out.print("So your name is " + name + "? Are you sure y/n : ");
            userYN = keyboardYN.nextLine().charAt(0);
            System.out.println();
            if(userYN == 'y')
            {
                System.out.println();
            }
            else
            {
                System.out.println("Type in your real name.");
            }

            }//end do
        while(userYN == 'n');

        //narration pt. 1
        System.out.println("You, " + name +
                           " have just been named the greatest, uh, what was it again?");
        System.out.println();

        //specialization
        System.out.print("Roll the dice to decide what your profession is? y/n : ");
        user2YN = keyboard2YN.nextLine().charAt(0);
        if(user2YN == 'y')
           {
            switch (dieRoll)
            {
                case '0':
                case '1':
                case '2': outputType = Mage;
                case '3':
                case '4':
                case '5': outputType = Soldier;
                case '6':
                case '7':
                case '8': outputType = Explorer;
                default : outputType = howTo;
            }//end switch
            System.out.println("Oh right, you are the greatest " + outputType + " in the town.");

            }
            else
            {
                System.out.println("I must be thinking of someone else then.");
            }

        //get quest


        System.out.println();
        System.out.println("End of program");
    }//end main
}//end class

============

The error message i get reads "variable Mage might not have been initialized."

I don't have much coding experience, and was wondering what I did wrong and how I could fix it in future programs.

  • What line/variable are you getting this error on? – Mureinik Sep 22 '15 at 22:06
  • Why do you have 3 scanner's on `System.in`? – Boris the Spider Sep 22 '15 at 22:06
  • First of all you have to initialize variables before using them. You can initialized it with `null` or other value. This variable have to be put in some state. If I were you I would use some IDE for easier coding. I recommend Intellij IDEA. It can be downloaded for free from Intellij website. – borysfan Sep 22 '15 at 22:14

1 Answers1

1

You have:

char Mage;
// ...
case '2': outputType = Mage;

What is the value of Mage at that point? The compiler is warning you that the variable has not been initialized.

You might want to initialize Mage to some value such as:

char Mage = '0';

Or most likely you want a String representation of Mage:

String outputType;
String mage = "Mage";
String soldier = "Soldier";
String explorer = "Explorer";
// ...
switch (dieRoll) {
    case '0':
    case '1':
    case '2': outputType = mage; 
              break;
    case '3':
    case '4':
    case '5': outputType = soldier; 
              break;
    case '6':
    case '7':
    case '8': outputType = explorer;
              break;
    default : outputType = "Oops";
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118