Reading Blank Lines into StringTokens into Vector Array

Discussion in 'Java' started by Row, Apr 14, 2004.

  1. Row

    Row Guest

    HI,
    I would first like to say its been about 3 years since looking at java
    im very rusty!
    I have to write a post it notes type applet which will function
    online. (reading from a flat text file)
    My main problem is: getting each paragraph into my vector array - so
    that each paragraph sits in a new array element. Eg: when i referance
    array elemant 2 it will give me paragraph which is in that element and
    not all paragraphs in the text file like my program is doing now

    Read from my text file which will look like this: (but with more
    entries)
    ------------------------------------------------------------------------------------------------------------------------------
    12/2/2000
    Post it note contents get put here and can span several lines
    depending on how they are input.
    Linke this

    3 blank lines - then the next entry....
    ------------------------------------------------------------------------------------------------------------------------------
    I hope i make sense!. As i said before i am really rusty on this...i
    sold my old java books so i am now spedning days on end trying to sort
    anything out. :(

    My code is below:

    /** Define the Method - which reads values from server and places them
    into textAreas **/
    void getList() {

    try {


    /** Define connection parameters **/
    URL url;
    URLConnection urlConn;
    DataInputStream dis;
    urlTx = urlText.getText();

    /** URL comes from text field **/
    url = new URL(urlTx);
    urlConn = url.openConnection();
    urlConn.setDoInput(true);
    urlConn.setUseCaches(false);

    //------------------------------------------------//
    // read the To-Do List data from the HTTP server //
    //------------------------------------------------//

    /** Define Data input stream after defining URL etc above **/
    dis = new DataInputStream(urlConn.getInputStream());
    String s;

    /** While not null take following actions **/
    while ((s = dis.readLine()) != null) {
    String data = new String(s + "\n");
    String line;

    /** Tokenize the string which as been input and read this string into
    an array**/
    StringTokenizer strtokLines = new StringTokenizer(data, "\n");

    /** vector to store String tokens in **/
    stringArray = new Vector();

    /** While has more tokens define "line" as next token and add "line"
    to Vector */
    while (strtokLines.hasMoreTokens()) {
    line = strtokLines.nextToken();
    stringArray.addElement(line);


    /*****
    * ERROR!!!!!
    * Attempt to output Vector below contents at element 0 - results on
    all tokens
    * being placed in first element of array (as seen below)
    * !!!!!
    */
    System.out.print(stringArray.elementAt(0));


    /** Test if Array actually has contents if not state so **/
    if ((stringArray.isEmpty() == true)) {
    System.out.println("Vector array is EMPTY!!");
    } else {
    //enumeration = vector contents
    Enumeration enum = stringArray.elements();
    //System.out.print(enum);
    }

    /** Whish to apend array contents to 2 different Textarea's on
    different cards here **/
    singleTextArea.setText(line);
    fullTextArea.setText(line);

    }
    }
    Row, Apr 14, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,732
  2. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    545
    James Kanze
    Sep 4, 2007
  4. Jim Burgess
    Replies:
    2
    Views:
    89
    Jim Burgess
    May 1, 2010
  5. Cah Sableng
    Replies:
    0
    Views:
    231
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page