We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

westerntour
westerntour asked
on
Medium Priority
434 Views
Last Modified: 2010-03-31
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?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Commented:
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
Commented:
oops! sorry objects! ;)

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

Bye, Giant.
Top Expert 2006

Commented:
> this is what happens inside the add method of ArrayList
it extends by factors of 2, not one by one

Author

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] =...
...

Commented:
>>it extends by factors of 2, not one by one

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

________
radarsh
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 ) ) ;

Commented:
>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[])
Top Expert 2006

Commented:
> 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;
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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]);

Author

Commented:
Please advise me the code

Commented:
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.
Top Expert 2006

Commented:
could someone tell me why others can post [full codes]
but when its mine, its extremely not allowed. Venabili is it democracy ?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> CEHJ, mayankeagle and objects got the deletions

I didn't post all of it, only some part (the rest was in the link).
Top Expert 2006

Commented:
your welcome ;-)

Commented:
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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.

Commented:
>>Sorry - yours is deleted also.

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

________
radarsh

Commented:
I know :) And I know that noone here had anything bad in mind :)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Venabili :-) ?

Author

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++;
 }
Top Expert 2006
Commented:
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(); );
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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()));
 }
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
>> 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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.