Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 03:48 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
list
how can we make a program that reads a list of words from a file (every line of the file having one word) and puts them in an array ? (supposing e.g. the words are 17 and setting the array size 20). using the buffered reader. (and the ACM library if necessary)

thank you
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007, 09:36 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.io.*; public class ReadingAFile { public static void main(String[] args) { String[] strs = new String[20]; int count = 0; BufferedReader br = null; try { File file = new File("readingAFile.txt"); br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); String line; while((line = br.readLine()) != null) { strs[count++] = line; } br.close(); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } for(int j = 0; j < count; j++) { System.out.println(strs[j]); } } }
readingAFile.txt
Code:
Waking Ned Devine No Country for Old Men The Postman Clearcut The Princess Bride
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 06:06 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
thank you man but it doesnt print out why? should it be something like j< count.length ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-20-2007, 08:22 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
The value of the int variable "count" increases every time another line is read so its final value should be the number of non–null elements in the "strs" array.
Try this:
Code:
while((line = br.readLine()) != null) { strs[count++] = line; System.out.println("count = " + count + " line = " + line); }
to see what is happening inside the while loop.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Members List Eranga Suggestions & Feedback 11 01-17-2008 11:41 AM
To display as a list yuvi461 New To Java 2 01-09-2008 01:06 PM
Linked List rnavarro9 New To Java 0 11-29-2007 04:42 AM
Link List one198 New To Java 0 10-14-2007 02:33 PM
How to get the max value from a list osval New To Java 1 07-30-2007 06:43 PM


All times are GMT +3. The time now is 09:40 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org