Need help w/ File input, read in line by line into array.

I need to prompt the user to enter a file name, read that file line by line.  Each line in the file has four parts seperated by a space.  Need to put these objects in an array.

I'm new to Java and programing, and am having a hard time figuring this out or finding examples.

Thanks,
Jim
jowens1023Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
BufferedReader rf = new BufferedReader(new FileReader("filename"));

String line = rf.readLine(); //reads a line from file

String[] parts = line.split("\s"); //splits line into strings, delimeter is white space (space, tab, new line, carriage return...)
vinlnxCommented:
And to read in the whole file and nothing more put what StillUnaware said: "String line = rf.readLine(); //reads a line from file" into a while loop and check if the file has any more lines to read like so:

while(rf.ready()){
String line = rf.readLine(); //reads a line from file
}
jowens1023Author Commented:
Thanks StillUnAware and vinlx,

That helps, but how do I get the lines into an array?

Thanks,
Jim
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

StillUnAwareCommented:
BufferedReader rf = new BufferedReader(new FileReader("filename"));
while(rf.ready()){
  ArrayList al = new ArrayList();
  al.add(rf.readLine()); //reads a line from file and adds to ArrayList
}
Object[] objArray = al.toArray();
String[] lines = new String[objArray.length];
for(int i = 0; i < objArray.length; i++)
  lines[i] = (String)objArray[i];

now 'lines' will contain lines from file 'filename'
zzynxSr. Software engineerCommented:
>> now 'lines' will contain lines from file 'filename'
No, it won't ;°)
Corrected (and shortened) code:

ArrayList al = new ArrayList();  // <<<<<<<< This one should be outside the loop
try {
        BufferedReader in = new BufferedReader(new FileReader("filename"));
        String line;
        while ((line = in.readLine()) != null)
            al.add(line);
        in.close();
    } catch (IOException e) {
    }
    String[] lines = (String[]) al.toArray(new String[0]);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
This *also* looks like homework - have you finished it now guys?
StillUnAwareCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.