Problem with using Vector


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem with using Vector

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    37

    Problem with using Vector

    There is a text file named 'stoplist' containing a list of words, like this:

    a
    the
    they
    we
    be
    is
    ...

    What I wanna do is to store them in a Vector. I've got the following code:

    Vector stopList = new Vector();
    File stoplist = new File("C:\\Text_Preprocessing\\stoplist.txt");
    FileReader stopword = new FileReader(stoplist);
    int s = stopword.read();
    while (s != -1) {
    StringBuffer StopString = new StringBuffer();
    if (s!='\n') StopString.append((char)s);
    else stopList.addElement(StopString.toString());
    }
    stopword.close();


    However, it can not be run! Where is the problem?

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    You need to catch IOException.

    Except I don't really see why you're going through all that trouble to read store lines. Here's a quicker way:
    Code:
    import java.util.*;
    import java.io.*;
    
    public class FileReaderExample {
        public static void main(String[] args) {
            ArrayList wordList = new ArrayList();
            try {
                BufferedReader in = new BufferedReader(new FileReader("someFile.txt"));
                String line;
                while ((line = in.readLine()) != null) {
                    wordList.add(line);
                }
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Useful links:
    The Java(tm) Tutorial: Handling Errors Using Exceptions
    Exceptions in Java
    Java Exceptions
    Exception Handling in Java
    Java Olympus: Java Exceptions
    Java Programming Notes: Exceptions
    Last edited by destin; 01-14-2006 at 07:09 PM.

Similar Threads

  1. Vector and StreamTokenizer problem
    By dagma20 in forum Java
    Replies: 5
    Last Post: 02-23-2005, 12:13 AM
  2. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 04:40 AM
  3. JTable problem
    By satish in forum Java
    Replies: 1
    Last Post: 02-23-2002, 01:44 AM
  4. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  5. Vectors and Event handling
    By Attiq in forum Java
    Replies: 1
    Last Post: 11-05-2000, 08:34 PM

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