Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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

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?

Update:

The programming language is C.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    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.

  • 5 years ago

    id be shocked if that they had any conscious concept decrease than no situations concepts cared i dont care what persons on surprising right here claimor what yahoo moderators perspectives are what i care approximately is yahoos strikes on it, i see a lot rubbish in surprising right here and what yahoo does approximately it somewhat is my seize 22 concern i see 'strategies/questions' from superb contributers which dont represent each physique who has contributed in a pragmatic way, yet they get 'rewarded' for having incorrect strategies and or trolling tds are long long long gone so as which you cant practice dislike anymore, so mutually as an respond is misguided you cant do some thing to demonstrate that to the persons analyzing it trolls commonly run this place, and a few of have tc badges , it truly is largely previous a humorous tale and as your asserting, they wont even learn your court docket situations/comments/letters, so all in all, cares, now no longer, a , jot, does, yahboousucks yahoo claims its approximately practise what it truly is speaks for itself

  • 1 decade ago

    what is the programming language ???

Still have questions? Get your answers by asking now.