We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,464 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

can anyone help me run this program?

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!");                                   
        }
    }
5
Contributors
8
Replies
10 Hours
Discussion Span
8 Months Ago
Last Updated
9
Views
jimmyjhonrubio
Newbie Poster
3 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

jimmyjhonrubio
Newbie Poster
3 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

jimmyjhonrubio
Newbie Poster
3 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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 =

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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();
stultuske
Industrious Poster
4,490 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 630
Skill Endorsements: 25
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

semicolon
Light Poster
25 posts since Jul 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0916 seconds using 2.82MB