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!");
}
}
jimmyjhonrubio 0 Newbie Poster
Recommended Answers
Jump to Postthe 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 …
Jump to PostJava variables must be initialize when you use them.
String name = ""; etc etcThey 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 …
All 8 Replies
NormR1 563 Posting Sage Team Colleague
jimmyjhonrubio 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
jimmyjhonrubio 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
stultuske 1,116 Posting Maven Featured Poster
semicolon 0 Light Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
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.