Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: Setting command line arguments in NetBeans. After importing the ZIP,

ID: 664628 • Letter: E

Question

Exercise 1: Setting command line arguments in NetBeans.

After importing the ZIP, run the file CommandLineDemo.java

At first, it has zero arguments. That is because we have not specified any. The arguments will be the names of your BFFs, or best friends forever.

Sample Output:

Number of BFFs is: 0

Setting arguments in the Terminal:

a. Arguments can be specified directly in the terminal, outside of NetBeans.

i. Compile the file. You may need to first use the cd command (change directory) to indicate the folder containing this file.

> javac CommandLineDemo.java

ii. Run the file with command line arguments appearing after the class name.

> java CommandLineDemo Angelina Brad Charles Command line arguments

b. Let's now do the same in NetBeans. The location of the window shown below is from a Mac. Expect small differences on a PC. On a Mac, under the Menu option Run, select Set Project Configuration and then choose Customize

In the field labeled Arguments, type the name of three friends. For example: Angelina Brad Charles Now, when you run the project in NetBeans, by clicking the green arrow, you should see an output like this. Sample Output:

run:

Angelina is one of my best friends.

Brad is one of my best friends.

Charles is one of my best friends.

Number of BFFs is: 3

Trouble Shooting: If you still do not see the command line arguments being used, be sure the Main Class for the project is set to CommandLineDemo Select the project Properties, then Run and use the Main Class field to do this.

Codes:

package hw;

public class CommandLineDemo {
  
public static void main(String[] args){
  
for(String friend : args) System.out.println(friend + " is one of my best friends.");
int numberOfFriends = args.length;

// Find the number of Best Friends Forever! (BFFs)
System.out.println("Number of BFFs is: " + numberOfFriends);

}

}

Explanation / Answer

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package helloworldapp; /** * * @author */ public class HelloWorldApp { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Hello World!"); } }