Question:

How to I read text file and store the data in array?

by  |  earlier

0 LIKES UnLike

Say I have data in the following format in text file

StudentID FirstName

1001 Adam

1002 Taylor

1008 Kelly

1005 Ryan

How do I read it and store it in array?

 Tags:

   Report

2 ANSWERS


  1. what is the programming language ???


  2. First you need a Buffered File Reader to take in the text:

    BufferedReader in = new BufferedReader(new FileReader("FILE DIRECTORY"));

    NOTE- Make sure there are no double slashes in your file directory because these are considered escape characters and will bring up errors when compiling.

    Next create your Array List:

    ArrayList<String> names = new ArrayList<String>();

    Next you will need to use a While Loop to fill the Array. While Loops are better for File Readers because, if coded right, will restrict the reader from trying to read past the last line in the file. Im assuming you don't want the line "StudenID FirstName" put into the array so I will take care of that by assigning a string to that line:

    String title = in.ReadLine();

    String example = in.ReadLine();

    while(!example.equals(null))

    {

    names.add(example);

    example = in.ReadLine();

    }

    If there are any problems with my code please correct.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.