[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

How I read the text file (line by line) and put them in array

I have a text file call "FBIN.text" like this
BIH      1608    1611+02 2017-06 2229-17 2127-28 223845 224455 223965
BLH 1714 1715+09 2115+03 2214-03 2422-16 2227-28 232744 253453 243665
FAT 1812 1815+05 1728+00 1923-07 2134-18 2140-29 214445 225056 224164
FOT 1733 1645-02 1837-05 1940-09 1951-19 1959-31 196647 197758 205558
ONT 2410 1710+05 2017+02 2320-05 2323-17 2127-28 233444 233754 243264

How to load them (line by line) and populate to the string array?
0
westerntour
Asked:
westerntour
  • 6
  • 5
  • 4
  • +5
6 Solutions
 
CEHJCommented:
0
 
objectsCommented:
follwoing shows how to read the file
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

add the lines to an ArrayList
and when done use it toArray() method to get your array
0
 
radarshCommented:
Hi westerntour,

You can use a BufferedReader and do a readLine.

As and when you read, you can put into an ArrayList and later convert to an Array.

Alternately, you can increase the size of the array by one each time you read a line
and copy values from the old array to the new one. (this is what happens inside the add method
of ArrayList)

________
radarsh
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
radarshCommented:
oops! sorry objects! ;)

________
radarsh
0
 
Giant2Commented:
Here an example how read and write:
http://www.java2s.com/Code/Java/File-Input-Output/Filereadandwrite.htm

Bye, Giant.
0
 
hoomanvCommented:
> this is what happens inside the add method of ArrayList
it extends by factors of 2, not one by one
0
 
westerntourAuthor Commented:
Hi radarsh,
How to write the code for this? Because the requirement is that I have to load all the lines and put them in array like this
list = new term[5]
list[0] = new term("BIH      1608    1611+02 2017-06 2229-17 2127-28 223845 224455 223965")
list[1] = new term("BLH 1714 1715+09 2115+03 2214-03 2422-16 2227-28 232744 253453 243665")
list[2] = ...
list[3] =...
...
0
 
radarshCommented:
>>it extends by factors of 2, not one by one

Nope! You can verify this by checking out the source of ArrayList ;)

________
radarsh
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Once you have read into an array-list using the link that objects provided:

int size = arrayList.size () ;
list = new term[size] ;

for ( int i = 0 ; i < size ; i ++ )
  list[i] = new term ( ( String ) arrayList.get ( i ) ) ;
0
 
Giant2Commented:
>How to write the code for this?
read the file using example we posted and, like objects told before:
>add the lines to an ArrayList
>and when done use it toArray() method to get your array

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#add(java.lang.Object)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#toArray(java.lang.Object[])
0
 
hoomanvCommented:
> Nope! You can verify this by checking out the source of ArrayList ;)
check the source again you'll find
ensureCapacity(size + 1)
would do
int newCapacity = (oldCapacity * 3)/2 + 1;
0
 
objectsCommented:
> How to write the code for this? Because the requirement is that I have to load all the lines and put them in array like this

where the process() method is in the link I posted earlier use:

mylist.add(new term(line));

where mylist is an ArrayList

hhen once doen:

term[] list = mylist.toArray(new term[0]);

0
 
westerntourAuthor Commented:
Please advise me the code
0
 
Giant2Commented:
the link I posted contains a program converting file to a Vector. From the Vector you can extract easily the String[] you want using the toArray() method:
String[] myarr=(String[]) theVector.toArray();

Bye, Giant.
0
 
hoomanvCommented:
could someone tell me why others can post [full codes]
but when its mine, its extremely not allowed. Venabili is it democracy ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
The code posted by objects is same as what is in the Java almanac link which he himself posted with only a minor tweak, so I guess this much is allowed.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> CEHJ, mayankeagle and objects got the deletions

I didn't post all of it, only some part (the rest was in the link).
0
 
hoomanvCommented:
your welcome ;-)
0
 
VenabiliCommented:
mayankeagle,

:) I just got all down. The Asker is supposed to try and post some code. Do you disagree? Otherwise we will get flooded with homeworks... and every expert will do a small part of real coding and.... the homework is ready..

Note to myself: I need to try to check all questions during the day and make sure I warb for homeworks earlier :)

hoomanv,

I cannot monitor all questions. That is why I have a mail in my profile and I have created this thread:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21798677.html

Venabili
0
 
Mayank SAssociate Director - Product EngineeringCommented:
No I don't disagree that we shouldn't post full-code. I try not to post full-code myself but only some parts of it.
0
 
radarshCommented:
>>Sorry - yours is deleted also.

:((  Well, sometimes we get over-enthusiastic and write full code!!

________
radarsh
0
 
VenabiliCommented:
I know :) And I know that noone here had anything bad in mind :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Venabili :-) ?
0
 
westerntourAuthor Commented:
Sorry, this is my first time here, so I don't know much your rule. Anyway, I give you the code that I did and give me advise
String wea[];
Scanner fileScan, urlScan;
fileScan = new Scanner(new File("FBIN.txt"));
while (fileScan.hasNext())
{
 wea[Count] = fileScan.nextLine();
 Count++;
 }
0
 
hoomanvCommented:
the code's logic is correct
but you have to use ArrayList instead of wea array
ArrayList would automatically extend its capacity whenever it reach to the end
but your code has not implemented that behaviour
and if the number of lines be more than the current capacity of wea array, you cant do any assignment to it
and you'll get an ArrayOutOfBoundsException

ArrayList<String> wea = new ArrayList<String>();
to add elements into it
wea.add(fileScan.nextLine(); );
0
 
objectsCommented:
if you want an array of term's as u mentioned earlier then you can create them in your loop
your code would then look like the following (also changing it to use a list)

List<term> wea = new ArrayList<term>();
Scanner fileScan, urlScan;
fileScan = new Scanner(new File("FBIN.txt"));
while (fileScan.hasNextLine())    /// should be hasNextLine() not hasNext()
{
 list.add(new term(fileScan.nextLine()));
 }
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> String wea[];

This is not initialized.

>> wea[Count] = fileScan.nextLine();

Likely to throw a compile error because of the above.

You will also not know how much space to allocate for wea because you don't know off-hand how many number of lines you have in the file, hence you have to use a List as shown.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now