|
|
(Yeah, I got this sales pitch from my real estate agent :-))
|
|
|
|
Therefore:
|
|
Open the file
Create a "Scanner" to read data
Read the input file into an array of String
Note: we must remember the number of words read !
|
import java.io.*;
import java.util.Scanner;
public class FormatText1
{
public static String[] words = new String[10000]; // Hold words in input file
public static int numWords; // Count # words in input
/* ===========================================================
readInput(in, w): read words from input "in" into array w
and return the #words read
============================================================ */
public static int readInput( Scanner in, String[] w )
{
int nWords = 0;
String x;
while ( in.hasNext() )
{
x = in.next(); // Read next word
w[nWords] = x; // Store it away
nWords++; // Count # words AND use next
// array element to store next word
}
return ( nWords );
}
public static void main(String[] args) throws IOException
{
if ( args.length == 0 )
{
System.out.println( "Usage: java FormatText1 inputFile");
System.exit(1);
}
File myFile = new File( args[0] ); // Open file "inp2"
Scanner in = new Scanner(myFile); // Make Scanner obj with opened file
/* ----------------------------
Read input from data file
---------------------------- */
numWords = readInput( in, words );
}
}
|
How to run the program:
|