Introduction
Welcome to JavaTown, a bustling city where everything runs on Java code! The mayor has just announced an exciting initiative to teach newcomers about user input in Java programming. As part of this initiative, they're looking for a simple program that demonstrates how to use the Scanner class to get user input.
You've been chosen to complete this special program! The basic structure has already been set up for you, and your task is to add the missing piece that reads the user's name using the Scanner class.
This program will be used to show new programmers in JavaTown how to interact with users through the console. Are you ready to help make JavaTown the most interactive place for new programmers? Let's get coding!
Complete the User Input Program
In this exercise, you'll be working on a simple yet fundamental aspect of interactive programming: taking user input. By completing this task, you'll enhance a basic Java program to personalize its greeting based on the user's name. Let's dive in and bring JavaTown to life with user interaction!
Tasks
- Open the pre-created file
WelcomeToJavaTown.javain the~/projectdirectory. - Find the TODO comment in the code.
- Add the line of code that uses the Scanner to read the user's name.
Requirements
- The file
WelcomeToJavaTown.javashould already exist in the~/projectdirectory. - You should only add one line of code where the TODO comment is.
- Use the existing
scannerobject to read the user's input. - Store the user's input in the
namevariable. - Use the
javaccommand to compile your program, and thejavacommand to run it.
Example
When completed correctly, your program should work like this when run:

cd ~/project
javac WelcomeToJavaTown.java
java WelcomeToJavaTown
Sample Output:
Enter your name: Labby
Hello, Labby! Welcome to JavaTown, where we love user input!
Summary
In this challenge, you've completed a Java program that demonstrates how to use the Scanner class to get user input. This exercise reinforced a key concept from your first Java lab: using the Scanner class for user interaction.
By completing this challenge, you've:
- Practiced using the Scanner class to read user input
- Learned how to integrate user input into program output
- Gained experience in reading and modifying existing Java code
This skill of getting user input is fundamental in creating interactive programs. As you continue your journey in JavaTown, you'll find that many programs need to interact with users, and the Scanner class is one of the primary tools for doing so in console applications.
Keep practicing and experimenting with your code. Try modifying the welcome message or asking for additional information from the user. The more you code, the more comfortable you'll become with Java. Welcome to the interactive world of programming in JavaTown!



