problems using StringTokenizer...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: problems using StringTokenizer...

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    2

    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();
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2005
    Posts
    2
    It wasn't a StringTokenizer problem after all. I was just trying to send a value to a constructor that didn't exist. Sorry for wasting your time guys.

Similar Threads

  1. Problems with MSSQLServer and Java
    By jlopes151 in forum Database
    Replies: 1
    Last Post: 11-21-2005, 04:32 AM
  2. Replies: 0
    Last Post: 07-19-2002, 01:41 AM
  3. Linux Installation problems
    By Mohammad Saleem in forum Open Source
    Replies: 2
    Last Post: 06-27-2002, 12:21 PM
  4. ASP Debuggin Problems in Visual Interdev
    By AndreasK in forum ASP.NET
    Replies: 0
    Last Post: 10-04-2000, 04:09 AM
  5. Replies: 0
    Last Post: 10-04-2000, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center