Results 1 to 4 of 4

Thread: list

  1. #1
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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
    Java Code:
    Waking Ned Devine
    No Country for Old Men
    The Postman
    Clearcut
    The Princess Bride

  3. #3
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    thank you man but it doesnt print out why? should it be something like j< count.length :confused: ?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •