// StudentFormat.java
// Rearranges columns in a text file using a StringTokenizer.
import java.util.StringTokenizer;
public class StudentFormat
{
public static void main(String[] args)
{
TextFileInput in = new TextFileInput("Students.txt");
String outputFilename = "Students-reformatted.txt";
TextFileOutput out = new TextFileOutput(outputFilename);
String line = in.readLine();
while ( line != null )
{
// Break up the line into meaningful chunks:
StringTokenizer st = new StringTokenizer(line);
// Extract ID number and insert hyphens:
String idNumber = st.nextToken();
idNumber = idNumber.substring(0,3) + '-'
+ idNumber.substring(3,5) + '-'
+ idNumber.substring(5);
// Extract the last and first names,
// (The first name may contain more than one token.)
String lastName = st.nextToken();
String firstName = st.nextToken();
while ( st.hasMoreTokens() )
firstName = firstName + " " + st.nextToken();
// Print with appropriate trailing spaces:
out.print(lastName);
for ( int i = lastName.length(); i < 15; i++ )
out.print(" ");
out.print(firstName);
for ( int j = firstName.length(); j < 10; j++ )
out.print(" ");
out.println(idNumber);
// Read NEXT line:
line = in.readLine();
} // while line
out.flush();
//Inform user that the file is finished:
System.out.println("File " + outputFilename + " has been created.");
} // method main(String)
} // class StudentFormat