import java.util.Scanner; // Needed for the scanner class

/**
    Chapter 2
    Programming Challenge
*/

    public class Wordgame
    {
        public static void main(String[] args)
         {
            String name;        //The user's name
            String age;         //The user's age
            String city;        //The name of a city
            String College;     //The name of a college
            String Profession;  //A profession
            String animal;      //A type of animal
            String petName;     //A pet's name

        // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner (System.in);

        // Get the user's name. 
            System.out.print ("Enter your name; ");
            name = keyboard.nextLine();

        // Get the user's age
            System.out.print ("Enter your age; ");
            name = keyboard.nextLine();

        // Get the name of a city.
            System.out.println ("Enter the name of a city; ");
            name = keyboard.nextLine();

        // Get the name of a College.
            System.out.println ("Enter the name of a college; ");
            name = keyboard.nextLine();

        // Get a Profession
            System.out.println ("Enter profession; ");
            name = keyboard.nextLine();

        // Get a type of animal.
            System.out.println ("Enter name of an animal; ");
            name = keyboard.nextLine();

        // Get a pet name.
            System.out.println ("Enter a pet name; ");
            name = keyboard.nextLine();

        // Display the "Story."
            System.out.println("There once was a person named" + name +
                                " who lived in " + city + ". At the age of ");
            System.out.println( age + ", " + name + "went to college at" + 
                             College + ", " + name.toUpperCase() + " graduated and went to live to ");
            System.out.println("work as a" + Profession.toLowerCase () + ". Then, " + name +
                                " adopted a(n) " + animal + " named " + petName);
            System.out.println("they lived hapilly ever after!");                                   
        }
    }

Please explain the problem you are having.
Post the complete text of any error messages
or the program's output and show what you want the output to be.

this is a basic program to create a sentence. and everytime i copile it i get "error: variable city might not have been initialized"
thanks

Where does the code assign a value to the variable: city? The compiler can not find where that is done, so it gives the error message.

The code asks the user for the name of a city and then reads the users response into a variable.
Should that be where city is given a value?

yeah, it replaces the value "city" with the users input,
i know i must have done something wromg but i cant figure out what

The compiler is telling you what is wrong: the variable city is not assigned a value.

Do you know what an assignment statement is? For example the following statement assigns the value "asdf" to the variable aStr:
aStr = "asdf";
The variable name is to the left of the = and the value assigned is to the right of the =

Look in the code for a statement that starts with: city =

the problem is, every time you enter data and assign a value, for no matter what type of info, you overwrite the value of name, but you don't set the values for the other variables.

for city, this part is what you should change:

// Get the name of a city.
            System.out.println ("Enter the name of a city; ");
            name = keyboard.nextLine();
import java.util.Scanner; // Needed for the scanner class
/**
    Chapter 2
    Programming Challenge
*/
    public class Wordgame
    {
        public static void main(String[] args)
         {

        //Initialize variables 
            String name = "";        //The user's name
            String age = "";         //The user's age
            String city = "";        //The name of a city
            String College = "";     //The name of a college
            String Profession = "";  //A profession
            String animal = "";      //A type of animal
            String petName = "";     //A pet's name
        // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner (System.in);
        // Get the user's name. 
            System.out.print ("Enter your name: ");
            name = keyboard.nextLine();
        // Get the user's age
            System.out.print ("Enter your age: ");
            age = keyboard.nextLine();
        // Get the name of a city.
            System.out.println ("Enter the name of a city: ");
            city = keyboard.nextLine();
        // Get the name of a College.
            System.out.println ("Enter the name of a college: ");
            College = keyboard.nextLine();
        // Get a Profession
            System.out.println ("Enter profession: ");
            Profession = keyboard.nextLine();
        // Get a type of animal.
            System.out.println ("Enter name of an animal: ");
            animal = keyboard.nextLine();
        // Get a pet name.
            System.out.println ("Enter a pet name: ");
            petName = keyboard.nextLine();
        // Display the "Story."
            System.out.println("There once was a person named " + name +
                                " who lived in " + city + ". At the age of ");
            System.out.println( age + ", " + name + " went to college at " + College + " , " + name.toUpperCase() + " graduated and went to live to ");
            System.out.println(" work as a " + Profession.toLowerCase () + ". Then, " + name +
                                " adopted a(n) " + animal + " named " + petName);
            System.out.println(" they lived hapilly ever after! ");                                   
        }
    }

Java variables must be initialize when you use them.

                String name = "";        
                String age = "";       
                String city = "";       
                String College = "";    
                String Profession = "";  
                String animal = "";      
                String petName = ""; 

here a sample http://stackoverflow.com/questions/1542824/c-initialization-of-instance-fields-vs-local-variables/1542851#1542851

Java variables must be initialize when you use them.
String name = ""; etc etc

They must be initialised before you use their value, but that does not mean that they must be, or should be, initialised when you declare them. Initialising like that is only useful when there is a specific initial or default value for the variable. In the case of this code each variable is assigned a value before it is used, so initialising them in the declarations is a complete waste of time.
In fact initialising in the declaration can be counter-productive because it effectively disables the compiler's checking for correct initialisation. It's far better not to initialise (except for known initial or default values) so the compiler can check your logic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.