Results 1 to 7 of 7

Thread: arrays help

  1. #1
    Warren is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default arrays help

    I am reading a text file with BufferedReader (FileReader).
    I want to put each line of the text (which has always one word per line)
    in an array(which size is predefined from the beginning and it is much bigger
    than the amount of the words of the .txt file). Whatever i try to solve the problem it always ends with this: 'cannot convert from String to String[]' :(


    so, how can I put these words one by one in the array? help plz
    Last edited by Warren; 11-22-2007 at 12:34 AM.

  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 ReadWords {
        public static void main(String[] args) {
            String[] receiver = new String[100];
            int count = 0;
            String path = "wordsToRead.txt";
            try {
                BufferedReader br = new BufferedReader(
                                    new FileReader(path));
                String line;
                while((line = br.readLine()) != null) {
                    receiver[count++] = line;
                }
                br.close();
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
            System.out.println("We read " + count + " lines from " + path);
            // Print the non-null elements of receiver.
            for(int j = 0; j < count; j++) {
                System.out.print(receiver[j]);
                if(j < count-1)
                    System.out.print(", ");
            }
            System.out.println();
        }
    }
    wordsToRead.txt
    Java Code:
    Read
    text
    from
    a
    character-input
    stream,
    buffering
    characters
    so
    as
    to
    provide
    for
    the
    efficient
    reading
    of
    characters,
    arrays,
    and
    lines.

  3. #3
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    	BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
    	String s;
    	String tmp = "";
    	while ((s = reader.readLine()) !=null) {
    		tmp += s + "\n";
    	}
    			
    	String[] arr = tmp.split("\\n");

  4. #4
    Warren is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Why we cant say this? (sorry for my ignorance)

    Java Code:
      try {
                BufferedReader br = new BufferedReader(
                                    new FileReader(path));
                
                while(br.readLine() != null) {
                    receiver[count++] = br.readLine()
                }

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

    Default

    Java Code:
    // The condition in the while loop argument reads
    // one line but does not save it
    while(br.readLine() != null) {
        // The next line is read here and is saved.
        receiver[count++] = br.readLine()
    }
    So this will read every other line: odd number lines are read but not saved, even number lines are read and saved. Another problem with this is that an exception will be thrown for files which have an even number of lines.
    Some like to write the read operation this way:
    Java Code:
    String line = br.readLine();
    while(line != null) {
        receiver[count++] = line;
        line = br.readline();
    }

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    What if the file is over a 100 lines?

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

    Default

    The easy way is to use a List or StringBuilder, add the lines as you go and convert to an array after the reading operation.
    Java Code:
    import java.util.List;
    import java.util.*;
    ...
    List<String list = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    
    String line;
    while((line = br.readLine()) != null) {
        list.add(line);
        sb.append(line + "\n");  // staykovmarin's suggestion
    }
    // convert to an array
    String[] words = list.toArray(new String[list.size()]);
             words = sb.toString().split("\n");

Similar Threads

  1. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  2. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  3. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 03:29 AM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  5. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:10 AM

Posting Permissions

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