• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a file into array?  RSS feed

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to read a text file that has contents like this


1 2
3 4
5 6
7 8
9 10


I want to read it into two arrays.First array should contain 1,3,5,7,9 and Second array should contain 2,4,6,8,10.

I have no idea how to do this

Any Help will be highly appreciated
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To start, can you tell us what you have learned so far?

Do you know how to open and read a file? Do you know how to declare and use arrays? optionally, collections? Do you know how to use loops?

Henry
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry Wong.
I know how to read and write files.Below is code written by me to read a file.I also know the use of arrays and loops but not collections

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay
I have found the solution.
Below is the code

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nelson Sam wrote:Hi Henry Wong.
I know how to read and write files.Below is code written by me to read a file.I also know the use of arrays and loops but not collections


Without the use of collections, you may have to read the files twice -- once to determine the number of elements, so that you can size the arrays. And once to actually read in and parse the elements. you should also look at some of the inputstream classes that can help you parse the file input.


Of course, this response is based on not being allowed to use collections because you haven't learn it yet. If this is not true, I recommend doing some research on them.

Henry
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed with Henry.

And aways put your "fr.close();" ind a Finally block. Or if you get an exception, your software will not release the file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!