how would I use print writer and file reader properly on an arraylistwith arraylist inside?

Discussion in 'Java' started by justineee, Nov 12, 2008.

  1. justineee

    justineee Guest

    Hi again,

    I have an ArrayList<Word> which creates a new word with an ArrayList
    of its definitions..

    I am trying to use readfile and writefile to save and load the words
    and definitions made by the user. It saves and loads, yes. However, it
    is not properly saved and loaded. I want to save the word with
    definitions as is and load it as is (so the user can still update the
    words definitions or add some words). I am not very familiar with
    PrintWriter and FileReader, I've been studying it for the past two
    weeks and all I can do is save and load an array.

    Example:

    I have..

    1. word
    --------------------
    [hello, word, thanks, again]
    --------------------

    -- this is the word and its definitions.

    after I save.. and load it again.. this shows


    1. word
    ------------------
    []-----------------------
    ----------
    2.[]---
    -------------------------
    []
    -----------------------
    3. [hello, word, thanks, again]

    here is my code for load and save.

    public void saveFile(ArrayList<Word> array)
    throws IOException
    {
    PrintWriter out = new PrintWriter ("initlist.txt");
    for (int ctr = 0; ctr < array.size(); ctr++)
    out.println(array.get(ctr));

    out.close();

    }


    public void loadFile(ArrayList<Word> array)
    throws IOException
    {
    array.clear();
    FileReader f = new FileReader ("initlist.txt");
    Scanner input = new Scanner (f);
    ArrayList<String> tempDef;
    while (input.hasNextLine())
    {
    String word = input.nextLine();

    if (array.contains(word)==false)
    {
    Word temp = new Word(word);
    array.add(temp);
    tempDef = new ArrayList<String>();

    for (int aCtr = 0; aCtr < tempDef.size(); aCtr++)
    {
    temp.addDefinition(tempDef.get(aCtr));
    }

    }


    }
    input.close();

    }


    I've been trying to fix this, I just can't :|

    Maybe anyone knows. Thanks.
     
    justineee, Nov 12, 2008
    #1
    1. Advertising

  2. justineee

    Guest

    Re: how would I use print writer and file reader properly on anarraylist with arraylist inside?

    On Nov 12, 2:55 pm, justineee <> wrote:

    >     tempDef = new ArrayList<String>();
    >
    >      for (int aCtr = 0; aCtr < tempDef.size(); aCtr++)
    >      {
    >       temp.addDefinition(tempDef.get(aCtr));
    >      }


    You have a bug there, you've just initialized tempDef to a new empty
    ArrayList, and try to iterate over it. No temp.addDefinition(..) will
    be performed.

    That's a start. (And it doesn't mean that without this bug the rest is
    correct).
     
    , Nov 12, 2008
    #2
    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. Roger Flores

    ANNOUNCE: new XML reader and writer

    Roger Flores, Feb 13, 2004, in forum: XML
    Replies:
    0
    Views:
    444
    Roger Flores
    Feb 13, 2004
  2. DaVinci
    Replies:
    1
    Views:
    568
    Piotr Kobzda
    Oct 5, 2006
  3. Guilherme Grillo

    reader inside a reader

    Guilherme Grillo, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    528
    sloan
    Nov 7, 2007
  4. Henri
    Replies:
    1
    Views:
    382
    Henri
    Jul 13, 2004
  5. Madhan Nedumaran

    Ini file reader/writer

    Madhan Nedumaran, Dec 24, 2008, in forum: Ruby
    Replies:
    1
    Views:
    244
    Dejan Dimic
    Dec 24, 2008
Loading...

Share This Page