Results 1 to 13 of 13
  1. #1
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default trouble inporting from a file

    im having trouble displaying a value from an imported file, the file is in the same folder as the project.
    the code im using for importing is this

    public String[] fillAuthor(){
    int counter = 0;
    String authors[] = new String [counter];
    String temp;
    try{
    FileInputStream fstream = new FileInputStream("titles.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    while ((temp = br.readLine()) != null) {
    counter++;
    authors[counter].equals(temp);
    }
    in.close();

    }
    catch (Exception e){

    }

    return authors;
    }

    the array gets passed to another method and the error is in the display line

    System.out.println(author[0]);

    the error is this

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0

    my best guess is that it isnt reading the file but im not sure why, the method that displays the array is called like this

    bookDisplay(fillAuthor());// fillAuthor reads the file and fills the array, bookDisplay uses the array

    i am told there are no errors until the code is run
    any help you guys could provide would be much appreciated

  2. #2
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: trouble inporting from a file

    I see 2 problems in your code.

    The first problem is that you increment the variable, counter by 1 before you use it with the authors[]. If you increment counter in this way, authors[0] will always be null. author[0] being null should be the cause of the problem you've described.

    Second when you assigning temp to a position in the authors[] your sending the message equals(temp) to the authors[] array. This doesn't make much sense because the equals method will return a boolean and not assign a String object to the authors[]. What you should have done there is use authors[counter] = temp;

  3. #3
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    hey so i tried fixing both of those mistakes you found the first way was this
    authors[counter] = (temp);
    counter++;

    the other ways was this
    counter++;
    authors[counter-1] = (temp);

    but im still getting the same error

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: trouble inporting from a file

    java.lang.ArrayIndexOutOfBoundsException: 0
    Is this the error you are getting?
    The error says that this is NOT an element at the index of 0.

    Where do you create the array? How many elements do you give it?
    How large is the array you are trying to index?
    Print out its length using the .length attribute to see.

  5. #5
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    I've been messing around with it and now it displays element 0 but nothing else, heres what i have for filling the array
    public String[] fillAuthor(){
    int counter = 0;
    String authors[] = new String [1+counter];
    String temp;
    authors[0] = "null";
    try{
    FileInputStream fstream = new FileInputStream ("authors.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader (new InputStreamReader(in));

    while ((temp = br.readLine()) != null) {
    counter++;
    authors[counter-1] = temp;

    }
    in.close();

    }
    catch (Exception e){
    //authors[counter].equals("error");
    }

    return authors;
    }

    i know i set element 0 to "null" but it gets set to the thing on the first line of the text file, so it works for the first line but not further, so i think its not reading the textfile right
    if im right, i have no idea what to do
    Last edited by mnorman; 12-11-2011 at 06:53 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: trouble inporting from a file

    Java Code:
    int counter = 0;
    String authors[] = new String [1+counter];
    the value of counter will always be 0 when the array is created.
    How many elements do you want in the array?

    What errors do you get when you execute this?

  7. #7
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    i tried the println you suggested and the result was the element if the first line of the text file repeated twice (theres 26 elements in the text file each on its own line) so theres deffinatly something wrong with how its reading the text file

  8. #8
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    the array needs to be resizable because the amount of elements in the text file will change

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: trouble inporting from a file

    arrays are not resizable. They get their size when they are created using the values of the variables at that time.

  10. #10
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    not to sound cocky or disrespectful but i know that, so thats why i set it to counter and have counter increasing by 1 every time theres a new line in the text filefile here
    ((temp = br.readLine()) != null) {
    counter++;
    authors[counter-1] = temp;

    or at least i though that was what was going on :p

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: trouble inporting from a file

    increasing the value of the index does not change the size of the array.
    Why do you add one to the index and then subtract one from it ?
    Why not use it and then add one?

  12. #12
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: trouble inporting from a file

    so is there any way to make the array resizable or should i just make the array really big?
    i tried both ways and it seamed to make no difference sso i went with that way

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: trouble inporting from a file

    way to make the array resizable See post#9
    Either make the array large enough to hold all possible input or use a class like ArrayList to hold the Strings.

Similar Threads

  1. trouble with FILE I/O
    By dru36 in forum New To Java
    Replies: 27
    Last Post: 12-02-2011, 09:25 PM
  2. Log file trouble
    By kovalensue in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2011, 05:13 PM
  3. Having trouble opening and reading a txt file
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 01-29-2011, 02:55 AM
  4. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 AM
  5. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 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
  •