### Array of the type String

• Introduction

• We have seen many examples of:

 arrays of doubles

• Array of other data types can be defined using very similar syntax as that used in arrays of double.

• In this webpage, we will show you the syntax used to define and access arrays of Strings

• Previously discussed: defining and using an array of double

• Syntax to define an array of double:

 ``` Step 1: double[] a; // The type "double[]" is the // "array object reference" type // The variable a contains an address of // an array of doubles Step 2: a = new double[5] ; // new double[5] creates an array // of 5 elements of double type // The array elements are named: // a[0], a[1], a[2], a[3], a[4] // Each element is of the type double ```

• Important facts:

• An individual array element of the array a is is accessed using the syntax:

 ``` a[ array-index ] ```

• An individual array element of the array double a is a variable of the data type double:

 ``` a[0] is a double typed variable a[1] is a double typed variable and so on... ```

Conclusion:

 Anywhere we can use a double typed variable, we can use an individual array element a[i] !!! (Because a[i] is a double typed variable)

• Now let us extrapolate these fact to an array of String

(And we will have learned how to define/access arrays of String)

• Defining an array of String

• Syntax to define an array of String:

 ``` Step 1: String[] a; // The type "String[]" is the // "array object reference" type // The variable a contains an address of // an array of Strings Step 2: a = new String[5] ; // new String[5] creates an array // of 5 elements of String type // The array elements are named: // a[0], a[1], a[2], a[3], a[4] // Each element is of the type String ```

• Using an array of String

 ``` public class String01 { public static void main(String[] args) { String x; x = "Hello World"; // Assign to a String typed variable System.out.println(x); // Print a String typed variable } } ```

Now apply the fact: Each array element a[i] is a String typed variable !!!

• Since each String array element is a String typed variable, we can conclude from the above example:

• How to assign and print a String array element a[0]:

 ``` a[0] = "Hello World"; // Assign to a String array element System.out.println( a[0] ); // Print a String array element ```

• Example program using String array:

 ``` public class String1 { public static void main(String[] args) { /* ------------------------------------- Create a String array of 5 elements ------------------------------------- */ String[] a; a = new String[5]; /* ------------------------------------- Assign a string to each array element ------------------------------------- */ a[0] = "Hello"; a[1] = "World"; a[2] = "How"; a[3] = "are"; a[4] = "you"; /* --------------------------------------------- Print the array elements (with a for loop) --------------------------------------------- */ int i; for ( i = 0; i < a.length; i++ ) { System.out.print( a[i] + " "); } System.out.println(); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac String1.java To run:          java String1

Output of this program:

 ``` Hello World How are you ```

• Internal representation of array of String

• The internal representation of an array of String is a bit complicated

But is it not crucial that you know the internal representation

• In fact, when we use variables in the Java's library, we often do not know (nor care to know) the internal representaion

 It is more crucial to know how to use a data structure.

• Consider the previous Java program:

 ``` public class String1 { public static void main(String[] args) { /* ------------------------------------- Create a String array of 5 elements ------------------------------------- */ String[] a; a = new String[5]; /* ------------------------------------- Assign a string to each array element ------------------------------------- */ a[0] = "Hello"; a[1] = "World"; a[2] = "How"; a[3] = "are"; a[4] = "you"; /* --------------------------------------------- Print the array elements (with a for loop) --------------------------------------------- */ int i; for ( i = 0; i < a.length; i++ ) { System.out.print( a[i] + " "); } System.out.println(); } } ```

• For the curious student, the following diagram depicts - step-by-step - what happens in the above program:

Note:

• The arrow from a variable X to a variable Y, represents a reference:

• Reading in a text file and store the words in an array of String

• Problem description:

 Write a program that reads in a text file (file containing words) Store each word in the file (separately) in an element of an array of String. Store the number of words in the input file in the variable numWords. You can assume that the file contains at most 100 words.

Example:

• Input file contains:

 ``` Hello World How are you ```

• The array of String will contains:

 ``` numWords = 5 a[0] = "Hello" a[1] = "World" a[2] = "How" a[3] = "are" a[4] = "you" a[5] = NULL a[6] = NULL .... a[99] = NULL ```

• Construct a Scanner object using an opened file:

 ``` /* --------------------------------- Open a data file --------------------------------- */ File myFile; // Define a "File" type variable to receive // the opened file myFile = new File("Path-name-of-the-file"); // Open the file /* -------------------------------------------------- Construct a Scanner from the opened file "myFile" -------------------------------------------------- */ Scanner in; // Define a Scanner typed variable in = new Scanner(myFile); // Construct a Scanner that read // data from opened file"myFile" ```

• From this point onwards, you can use

 in.nextDouble() to read a floating point number from the data file in.nextInt() to read an integer number from the data file in.next() to read a string (word) from the data file

• Previously discussed: print the content of a (numeric) data file (see: click here )

• Rough algorithm:

 ``` Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { read a word from the Scanner object; print the word; } ```

• Java program:

 ``` import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file String x; // Variable to receive a string while ( in.hasNext() ) { x = in.next(); // Read a string (word) System.out.println(x); // Print string read } System.out.println("Done"); } } ```

• Rough algorithm for reading in words from files into an array of String:

 ``` a = String array of 100 elements; int numWords = 0; Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { a[numWords] = read next word from the Scanner object; numWords++; // Use the NEXT element in array to store // the next word in file ! } /* -------------------------------------------------------------- We can process the words in the array here if we so desire... -------------------------------------------------------------- */ ... (further array processing) ... ```

• Java program:

 ``` import java.io.*; import java.util.Scanner; public class ReadText1 { public static void main(String[] args) throws IOException { String[] a = new String[100]; // Create String array of 100 elem's int numWords; // Count # words in input File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file numWords = 0; while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } System.out.println("Printing the array..."); for ( int i = 0; i < numWords; i++ ) System.out.println( a[i] ); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac ReadText1.java To run:          java ReadText1

 ``` while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } ```
 ``` while ( in.hasNext() ) { a[numWords++] = in.next(); } ```