Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By jim829

Thread: Reading a file in a string array

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Reading a file in a string array

    How do I read in a file line by line into an array without using arraylist?

    I know how to do this using BufferedReader, but I am wondering how to do this using Scanner? When I used BufferedReader I noticed that there must be two exceptions to be caught which were IOException and FileNotFoundException, whereas a Scanner needs only a FileNotFoundException, why is that?

    Java Code:
    public class practice {
        public String[] array;
        Scanner inputStream = null;
        Scanner n = new Scanner(System.in);   
        public String line;
      
        public practice(String theFile) {       
            array = new String[150];
            try {           
                inputStream = new Scanner(new FileInputStream(theFile));
                line = inputStream.nextLine();
                while (inputStream.hasNextLine()) {
                    for (int i = 1; i < array.length; i++){
                        array[0] = line;
                        //dont know what to put here           
                     }
                 }             
             } catch(FileNotFoundException e) {
                 System.out.println(e.getMessage());
             }
             inputStream.close();     
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Reading a file in a string array

    Quote Originally Posted by Robben View Post
    How do I read in a file line by line into an array without using arraylist?
    The difference between using an array or a list - ArrayList or other - is that arrays have a size which does not change once you have created them. So you must set the size of the array *before* you populate it with strings as you do in the code you posted. This is a little awkward if you want to avoid both "running out" of array and bogus null values. (You could read the file twice, the first time just counting the number of lines.)

    I know how to do this using BufferedReader, but I am wondering how to do this using Scanner?
    You are already using hasNextLine() and nextLine() which seem to do what you want: ie, get a line from the fles, and test wheher such a line is available. But I don't really understand the logic you are using. Why not something along these lines:

    Java Code:
    int ndx = 0;
    while(there is a next line) {
        array[ndx] = read next line
        increment ndx
    }
    When I used BufferedReader I noticed that there must be two exceptions to be caught which were IOException and FileNotFoundException, whereas a Scanner needs only a FileNotFoundException, why is that?
    According to the API docs linked to above nextLine() will return the next line or it will throw NoSuchElementException. I am guessing that in the event of some random IO exception the scanner will fail to find a line to return and will throw a NoSuchElementException. I haven't tested this (eg by reading a CD and forcefully ejecting it half way through).

    Both the Scanner and FileReader constructors can throw a FileNotFoundException - rather than the more general IOException - when you construct them. They are alike in this respect.

    The BufferedReader readLine() method can throw a IOException. This makes sense because a buffered reader can be getting its data from all sorts of different streams (file, console, internet, etc) and there are a lot of different IO exceptions that could happen. It also makes sense that the scanner nextLine() would *not* use the IOException to report a problem with returning a line, since that failure might have nothing to do with an io exception but, rather, be a problem with the stream's contents. (I can't actually think of what that problem might be! But I can in the case of the other scanner nextXXX() methods, and perhaps the library writer just wanted to be consistent.)
    Last edited by pbrockway2; 03-22-2015 at 12:52 AM.
    Robben likes this.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading a file in a string array

    One thought on reading in an indefinite number of items. If you use ArrayList, then you don't need to know how many because ArrayList grows dynamically as needed to accommodate the number of input items. This is not necessarily more efficient, just easier since ArrayList is backed by an array and suffers the same problems that you would suffer using an array. So some reallocation and copying goes on behind the scenes as necessary. Another alternative is to use a LinkedList implementation. This simply adds nodes as you read in the data so no reallocation or copying is required. Its major inefficiencies are that it has nodal overhead and is a sequential access list (whereas ArrayLists are random access lists). Each has its merits based on how it is to be used. Just something to think about.

    Regards,
    Jim
    Robben likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2014, 12:27 AM
  2. Reading File for specific string
    By luigi10011 in forum New To Java
    Replies: 4
    Last Post: 06-01-2013, 02:59 AM
  3. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  4. reading and writing string on notepad file
    By janroz in forum New To Java
    Replies: 5
    Last Post: 01-19-2012, 12:18 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
  •