This tutorial takes up where the Hello World! in Java left off. Our next step will be to allow the user to supply their name at runtime to personalise the “Hello World!” greeting. We will use the final code from that tutorial (see below) as a starting point in our new program, HelloWorldArg.

In this tutorial:

  1. Arguments
  2. Run
  3. jGRASP
  4. Next steps

Required knowledge:

1. Arguments

The following program simply outputs a message to the screen:

class HelloWorld {
    public static void main(String[] arguments) {
        // this is just a comment
        System.out.print("Hello World!");
    }    
}

An argument is a value passed to a function when the function is called.

To add to the usefulness of the program, we are going to allow the user to enter their name as an argument to our program at runtime to personalise the greeting!

To enter user input at runtime, we use the following syntax at the Command Prompt: java NameOfProgram arguments1 arguments2 ...

To output the information in the program, we access the argument values using arguments[0]arguments[1] etc

The final code is therefore:

class HelloWorldArg {
    public static void main(String[] arguments) {
        // this is just a comment
        System.out.print("Hello World!\n");
        System.out.print("My name is " + arguments[0]);
    }    
}

LINE 4:\n” is an Escape Character in Java that is used to enter a new line in the output. [2]

LINE 5: the + concatenates (joins) 2 strings together

Are you remembering to compile and run your code to test for errors?.

2. Run

To run this program at the Command Prompt:

java HelloWordlArg Fox

or, for your full name including a space:

java HelloWorldArg "Mister Fox"

3. jGRASP

If you run a program that requires arguments at runtime in jGRASP:

Annotated screenshot of activation of the Run Arguments option in jGRASP.Annotated screenshot showing the Run Arguments option in jGRASP.
  1. Select the Build menu
  2. Select the Run Arguments option
  3. Supply the argument(s) in the Run Arguments field

4. Next steps

What happens if you run the program without supplying an argument? What happens if you supply more than one word?

Wouldn’t it be great if the message popped up in a window? Even better: a pop-up window for the user input?


References:

  1. Specifying Command-Line Arguments in jGrasp. (2023). Retrieved 29 January 2023, from https://kyledewey.github.io/comp110-fall17/resources/jgrasp_command_line_arguments/
  2. Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings) . (2023). Retrieved 30 January 2023, from https://docs.oracle.com/javase/tutorial/java/data/characters.html

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.