Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2005

Tip: Looking for answers? Try searching our database.

Simple Java Question (reading text file of integer values)

Thread view: 
TikkieTerug - 26 May 2005 19:37 GMT
For a project, we need to read a text file, that contains an integer
value on every line. We would like to store the integer values in an
array (the first integer being stored in arrayname[1]). Does anybody
have an idea how to write such a program?

We are grateful for any help we get! Thank you very much in advance.
Andrew Thompson - 26 May 2005 19:58 GMT
> For a project, we need to read a text file, that contains an integer
> value on every line. We would like to store the integer values in an
> array (the first integer being stored in arrayname[1]).

What do you intend doing with element arrayname[0]?

>...Does anybody have an idea how to write such a program?

Yes.

> We are grateful for any help we get!

Feel free to drop by once you formulate a specific question.
But please review your course notes, hack out some code[1],
and read the PhySci FAQ[2] carefully before replying.

[1] Have you done the HelloWorld program?
[2] <http://www.physci.org/codes/javafaq.jsp>

>..Thank you very much in advance.

You're very much welcome.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Fahd Shariff - 27 May 2005 15:58 GMT
Here is a quick hack. The lines are read into an arraylist which is
then copied to an array. The first integer is stored in arr[0] i.e.
integer k is stored in arr[k-1]. If thats not what you want, you can
fix it easily.

       BufferedReader in = new BufferedReader(new
FileReader("data.txt")) ;
       String line = "" ;
       ArrayList nums = new ArrayList() ;
       while((line = in.readLine())!=null){
           int num ;
           try{
               num = Integer.parseInt(line) ;
               nums.add(new Integer(num));
           }catch(NumberFormatException e){}
       }
       int[] arr = new int[nums.size()] ;
       for(int i = nums.size() ; --i>=0 ;){
           arr[i] = ((Integer)nums.get(i)).intValue() ;
       }

Signature

Fahd Shariff
"Let the code do the talking... "



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.