|
Step 1: double[] a; // The type "double[]" is the // "array object reference" type // The variable a contains an address of // an array of doubles |
|
(And we will have learned how to define/access arrays 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 |
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 !!!
|
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(); } } |
How to run the program:
|
Output of this program:
Hello World How are you |
But is it not crucial that you know the internal representation
|
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(); } } |
![]() |
Note:
|
|
Example:
|
|
|
|
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) ... |
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] ); } } |
How to run the program:
|
Advanced Java/C programmers write this loop:
while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } |
as follows:
while ( in.hasNext() ) { a[numWords++] = in.next(); } |
(because numWords++ evaluates to the old value of numWords).
|
Example:
|
|
Read in the words and store the words in an array "a" of String; // An array give us easy access to the words in the file // BTW, we have discussed this step previously /* -------------------------- Process every word -------------------------- */ line = ""; // Start with an empty sheet of paper for ( i = 0; i < numWords; i++ ) { if ( a[i] fits in line ) { add a[i] (+ a separating space) to line; } else { Print line; Start a new line with a[i] in the line; } } |
Refine the abstract description into programmable actions:
|
import java.io.*; import java.util.Scanner; public class FormatText1 { 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("inp2"); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file /* ----------------------------------------- Read in the text file and store in array ----------------------------------------- */ numWords = 0; while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } /* ===================================== Format algorithm ===================================== */ String line; // 1 line line = ""; // Start with empty line for ( int i = 0; i < numWords; i++ ) { if ( line.length() + 1 /* space */ + a[i].length() <= 30 ) { line = line + " " + a[i]; // Add a[i] to line } else { System.out.println(line); // Print line line = a[i]; // Start new line with a[i] } } } } |
How to run the program:
|
Result of the execution:
Input file | Output of program |
---|---|
We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. |
We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of |
|
import java.io.*; import java.util.Scanner; public class FormatText2 { 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("inp2"); // Open file "inp2" 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 } String line; // 1 line line = ""; for ( int i = 0; i < numWords; i++ ) { if ( line.length() == 0 ) { line = a[i]; } else if ( line.length() + 1 /* space */ + a[i].length() <= 30 ) { line = line + " " + a[i]; } else { System.out.println(line); // Next word can't fit line = a[i]; } } System.out.println(line); // Print last line } } |
How to run the program:
|
Result of the execution:
Input file | Output of program |
---|---|
We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. |
We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. |