Multidimensional Arrays Objectives Command-line arguments

J.E.D.I

7.6 Multidimensional Arrays

Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays are declared by appending the appropriate number of bracket pairs after the array name. For example, integer array 512 x 128 elements int[][] twoD = new int[512][128]; character array 8 x 16 x 24 char[][][] threeD = new char[8][16][24]; String array 4 rows x 2 columns String[][] dogs = {{ terry, brown }, { Kristin, white }, { toby, gray}, { fido, black} }; To access an element in a multidimensional array is just the same as accessing the elements in a one dimensional array. For example, to access the first element in the first row of the array dogs, we write, System.out.print dogs[0][0] ; This will print the String terry on the screen. Introduction to Programming I 119 J.E.D.I

7.7 Exercises

7.7.1 Days of the Week

Create an array of Strings which are initialized to the 7 days of the week. For Example, String days[] = {“Monday”, “Tuesday”….}; Using a while-loop, print all the contents of the array. do the same for do-while and for- loop

7.7.2 Greatest number

Using BufferedReader or JOptionPane, ask for 10 numbers from the user. Use an array to store the values of these 10 numbers. Output on the screen the number with the greatest value.

7.7.3 Addressbook Entries

Given the following multidimensional array that contains addressbook entries: String entry = {{Florence, 735-1234, Manila}, {Joyce, 983-3333, Quezon City}, {Becca, 456-3322, Manila}}; Print the following entries on screen in the following format: Name : Florence Tel. : 735-1234 Address : Manila Name : Joyce Tel. : 983-3333 Address : Quezon City Name : Becca Tel. : 456-3322 Address : Manila Introduction to Programming I 120 J.E.D.I 8 Command-line Arguments

8.1 Objectives

In this section, we will study on how to process input from the command-line by using arguments pass onto a Java program. At the end of the lesson, the student should be able to: • Know and explain what a command-line argument is • Get input from the user using command-line arguments • Learn how to pass arguments to your programs in NetBeans

8.2 Command-line arguments

A Java application can accept any number of arguments from the command-line. Command-line arguments allow the user to affect the operation of an application for one invocation. The user enters command-line arguments when invoking the application and specifies them after the name of the class to run. For example, suppose you have a Java application, called Sort, that sorts five numbers, you run it like this: Take note that the arguments are separated by spaces. Introduction to Programming I 121 Figure 8.1: Running with Command-Line Arguments J.E.D.I In the Java language, when you invoke an application, the runtime system passes the command-line arguments to the applications main method via an array of Strings. Each String in the array contains one of the command-line arguments. Remember the declaration for the main method, public static void main String[] args The arguments that are passed to your program are saved into an array of String with the args identifier. In the previous example, the command-line arguments passed to the Sort application is an array that contains five strings which are: 5, 4, 3, 2 and 1. You can derive the number of command-line arguments with the arrays length attribute. For example, int numberOfArgs = args.length; If your program needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as 34, to a number. Heres a code snippet that converts a command-line argument to an integer, int firstArg = 0; if args.length 0{ firstArg = Integer.parseIntargs[0]; } parseInt throws a NumberFormatException ERROR if the format of args[0] isnt valid not a number. Coding Guidelines: Before using command-line arguments, always check if the number of arguments before accessing the array elements so that there will be no exception generated. Introduction to Programming I 122 J.E.D.I

8.3 Command-line arguments in NetBeans