The aim of this exercise is to expand on your original program by allowing simple interaction with a user. The sequence is as follows:
- The user provides their first and last names as command line arguments when running the program.
- The user is then greeted by their full name.
Note
Like before, this exercise should be solved using the command line and a simple text editor only to get an understanding of what command line arguments are under the hood.
You already have most of the information needed to write this program, using print statements for step 2. However, we need to know how Java reads in arguments from the command line. When the program starts, these arguments are stored in the parameter args which occurs as part of the main() method. Don’t worry about the details, for the moment you just need to know that the first argument can be accessed through args[0], the second through args[1], and so forth.
So, if we started the program as
java PersonalGreeting Hello World
then args[0] would contain the string "Hello", and args[1] would contain the string "World".
To start, create a new class called PersonalGreeting. As before you need to create a main() method and write your new code inside the definition block of the main() method (i.e., inside the pair of curly brackets that immediately follow the parameter list ( String[] args )).
public class PersonalGreeting { public static void main( String[] args ) { ... } }
To make our program clearer, we will save the values from args[0] and args[1] into a couple of variables. To do this, we need to declare the variables. A variable declaration consists of the type of the item being stored, in this case a String, followed by the name of the variable. The following snippet first declares that we have a variable called first_name of type String, and secondly uses the variable to store the value of args[0], namely the first word typed after the command java PersonalGreeting.
String first_name; first_name = args[0];
If we like, we can collapse these two separate statements into a single one, as shown below:
String first_name = args[0];
We will do this for both the first and last names:
String first_name = args[0]; String last_name = args[1];
Finally we need a way of joining two strings together; this is done with the + symbol, as shown:
"My Name is " + "Bob Johnson" // Joins two strings together "My Name is " + name // Joins the first string to variable called 'name'
Using this information, you should be able to perform step 2 and complete the whole exercise. Write your class and main() method to do so, then compile and execute it using the same procedure as before. Remember, if your compilation fails, you will have to go back and correct any errors you have made before being able to continue.
If everything has gone well, your session at the terminal should look something like this:
: javac PersonalGreeting.java : java PersonalGreeting Suzy Green Hello Suzy Green :
An automated test has been created for this exercise: PersonalGreetingTest.java.
How to use this test with IntelliJ to check your program for correctness is described in the Introduction to Development Tools.