File Reader and Array loop problem

J

justineee

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.
 
S

Simon

justineee said:
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.
 
M

Martin Gregorie

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.
 
J

justineee

justineee said:
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.
 
L

Lars Enderin

justineee said:
justineee said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top