Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Read text file to array split by space/new line

807600May 20 2007 — edited May 20 2007
Hi Guys/gals,
I have atext file that I am tring to read to an array. So far I have it so it reads each line however I cannot work out how to read each "word" so to speak. I need the space in my file to be the split. I know i need to use the split(); function but I am unsure where to put it.

If you could let me know where and how I can improve the code i have currenty that would be great. I am not sure if its 'good'...
cheers.

Text file looks like this : (There are more lines but always 6 objects with

John doe 1 2 3 Smith
Jane doe 1 2 3 Smith

import java.io.*;
import java.util.Vector;

public class reader {
    public static void main (String args[]){
        String ln;
        String[] apps;
        Vector lines = new Vector();

        try{
            FileReader fr=new FileReader("applicants.txt");
            BufferedReader br=new BufferedReader(fr);

            while((ln=br.readLine())!=null){
                lines.addElement(ln);
            }


            apps = new String[lines.size()];

            for (int i = 0; i < apps.length; i++) {
                apps[i] = (String) lines.elementAt(i);

                // Test to see if the array works.
                System.out.println(apps);
}
}
catch(IOException e) {
System.out.println("Error: " + e);
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2007
Added on May 20 2007
3 comments
2,552 views