1 Attachment(s)
problems using StringTokenizer...
In my program, I'm trying to read lines of strings from a text file and use StringTokenizer to store separate tokens into arrays. As you can see from my code below, I'm trying to store a line of string as tokens into the array fields by last name, first name, id, and password. This particular method code works in the general sense but I keep getting returned null values in my text file. Does anyone know what I'm doing wrong because everytime I execute the method, the outfile is completely erased because of the null. I caught the nullpointer exception but that doesn't solve my problem....The text file I'm reading from is attached.
void executeFile()
{
// ************* Create infile ************
FileInput inFile = new FileInput(myFileName);
for(int i=0; i<44; i++)
{ // READ File loop
read = inFile.readLine();
StringTokenizer myLine = new StringTokenizer(read);
myLast = myLine.nextToken();
myFirst = myLine.nextToken();
myID = myLine.nextToken();
myPassword = myLine.nextToken();
amendClass[i] = new STUDENT(myLast, myFirst, myID, myPassword);
}
for(int i=8; i<44; i++)
{
// locate first non record and replace with new Student
if ((amendClass[i].myLast).equals("zzz"))
{
amendClass[i] = new STUDENT(myLast, myFirst, myID, myPassword);
i=46; // terminates loop
}
}
inFile.close();
// Great place to Sort Amended Array so that new student is
//in Alphabetical Order
STUDENT temp = new STUDENT("#","#","#","#");
for (int j = 8; j < amendClass.length - 1; j++)
{
for (int k = 8; k < amendClass.length-j-1; k++)
{
if ((amendClass[k].myLast).compareTo(amendClass[k + 1].myLast) > 0)
{
temp = amendClass[k];
amendClass[k] = amendClass[k + 1];
amendClass[k + 1] = temp;
}
}
}
FileOutput outFile = new FileOutput(myFileName); // Write changes to File
try
{
for(int i=0; i<44; i++)
{
outFile.println(Format.left(amendClass[i].myLast, 15) +
Format.left(amendClass[i].myFirst, 15) +
Format.left(amendClass[i].myID, 15) +
Format.left(amendClass[i].myPassword, 15));
}
}
catch(NullPointerException a)
{
outFile.println(Format.left("zzz", 15) +
Format.left("No", 15) +
Format.left("Student", 15) +
Format.left("zzz", 15));
System.out.println("Caught an exception");
}
outFile.close();
}