File Reader and Array loop problem

Discussion in 'Java' started by justineee, Oct 29, 2008.

  1. justineee

    justineee Guest

    Hi Everyone,

    I created this method..

    public void loadFile(String[] array)
    throws IOException
    {
    int nWords=0;
    FileReader f = new FileReader ("initlist.txt");
    Scanner input = new Scanner (f);
    while (input.hasNextLine())
    {
    String blah = input.nextLine();
    int lCtr = 0;
    array[lCtr] = blah;
    lCtr++;
    }
    input.close();
    }

    my problem is that the last line is being put in the '0' index of my
    array.. I want it to function like one element per line.. I can't
    think of other ways to deal with this.

    Can somebody help me? Please I'm desperate :(
    Thanks.
    justineee, Oct 29, 2008
    #1
    1. Advertising

  2. justineee

    Simon Guest

    justineee wrote:
    > public void loadFile(String[] array)
    > throws IOException
    > {
    > int nWords=0;
    > FileReader f = new FileReader ("initlist.txt");
    > Scanner input = new Scanner (f);
    > while (input.hasNextLine())
    > {
    > String blah = input.nextLine();
    > int lCtr = 0;


    Declare lCtr outside the while loop. You are resetting it to 0 in each
    iteration. My guess is that you originally intended to use nWords for this
    purpose which appears to be unused.

    > array[lCtr] = blah;
    > lCtr++;
    > }
    > input.close();
    > }
    >
    > my problem is that the last line is being put in the '0' index of my
    > array.. I want it to function like one element per line.. I can't
    > think of other ways to deal with this.
    >
    > Can somebody help me? Please I'm desperate :(
    > Thanks.
    Simon, Oct 29, 2008
    #2
    1. Advertising

  3. On Wed, 29 Oct 2008 08:31:56 -0700, justineee wrote:

    > Hi Everyone,
    >
    > I created this method..
    >
    > public void loadFile(String[] array)
    > throws IOException
    > {
    > int nWords=0;
    > FileReader f = new FileReader ("initlist.txt"); Scanner

    input = new
    > Scanner (f);
    > while (input.hasNextLine())
    > {
    > String blah = input.nextLine();
    > int lCtr = 0;
    > array[lCtr] = blah;
    > lCtr++;
    > }
    > input.close();
    > }
    >
    > my problem is that the last line is being put in the '0' index of my
    > array.. I want it to function like one element per line.. I can't think
    > of other ways to deal with this.
    >

    Currently the value of lCtr is following the sequence
    0, 1, 0, 1, 0, 1, ............

    and every line is going into the zeroth element of 'array'. A moment's
    code reading should show you why that's happening. If you're still
    baffled, put the line

    System.out.println("array[" + lCtr + "] = " + array[lCtr]);

    immediately in front of the "lCtr++;" line and run it again.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Oct 29, 2008
    #3
  4. justineee

    justineee Guest

    On Oct 29, 11:41 pm, Simon <> wrote:
    > justineee wrote:
    > > public void loadFile(String[] array)
    > >            throws IOException
    > >    {
    > >            int nWords=0;
    > >            FileReader f = new FileReader ("initlist.txt");
    > >            Scanner input = new Scanner (f);
    > >            while (input.hasNextLine())
    > >            {
    > >                    String blah = input.nextLine();
    > >                    int lCtr = 0;

    >
    > Declare lCtr outside the while loop. You are resetting it to 0 in each
    > iteration. My guess is that you originally intended to use nWords for this
    > purpose which appears to be unused.
    >
    > >                    array[lCtr] = blah;
    > >                    lCtr++;
    > >            }
    > >            input.close();
    > >    }

    >
    > > my problem is that the last line is being put in the '0' index of my
    > > array.. I want it to function like one element per line.. I can't
    > > think of other ways to deal with this.

    >
    > > Can somebody help me? Please I'm desperate :(
    > > Thanks.

    >
    >


    Oh yeah thanks! I'm so stupid. hahaha.
    justineee, Oct 29, 2008
    #4
  5. justineee

    Lars Enderin Guest

    justineee wrote:
    > On Oct 29, 11:41 pm, Simon <> wrote:
    >> justineee wrote:
    >>> public void loadFile(String[] array)
    >>> throws IOException
    >>> {
    >>> int nWords=0;
    >>> FileReader f = new FileReader ("initlist.txt");
    >>> Scanner input = new Scanner (f);
    >>> while (input.hasNextLine())
    >>> {
    >>> String blah = input.nextLine();
    >>> int lCtr = 0;

    >> Declare lCtr outside the while loop. You are resetting it to 0 in each
    >> iteration. My guess is that you originally intended to use nWords for this
    >> purpose which appears to be unused.
    >>
    >>> array[lCtr] = blah;
    >>> lCtr++;
    >>> }
    >>> input.close();
    >>> }
    >>> my problem is that the last line is being put in the '0' index of my
    >>> array.. I want it to function like one element per line.. I can't
    >>> think of other ways to deal with this.
    >>> Can somebody help me? Please I'm desperate :(
    >>> Thanks.

    >>

    >
    > Oh yeah thanks! I'm so stupid. hahaha.

    You also have to think about how you define the array and make sure that
    it has room for all the lines of the file. It would probably be better
    to have the loadFile method return an array or a list, and to create a
    list by reading from the file.
    Lars Enderin, Oct 29, 2008
    #5
    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. Guilherme Grillo

    reader inside a reader

    Guilherme Grillo, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    526
    sloan
    Nov 7, 2007
  2. justineee
    Replies:
    1
    Views:
    2,569
  3. jason
    Replies:
    13
    Views:
    876
    John B. Matthews
    May 14, 2010
  4. Pokkai Dokkai
    Replies:
    1
    Views:
    233
    Hassan Schroeder
    Mar 24, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page