Results 1 to 16 of 16
  1. #1
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Reading a text file into an Array and spliting the content into another Array

    Hello.

    I have wrote some code to read atext file line by line.
    As it is reads each line, the string is added to an Array.
    Afterwards, it splits the contents into another Array.

    The only problem is, it doesn't work.
    All the lines are stored into the Array without a problem.

    Can someone please tell me how to fix this :confused: Thanks!

    Java Code:
            String[] myarray = new String[5];
            String[][] subarray= new String[5][10];
            BufferedReader br = new BufferedReader(new FileReader(text file));
            for (int j = 0; j < myarray.length; j++) {
                myarray[j] = br.readLine();
                System.out.println(myarray[j]);
                for (int i = 0; i < myarray.length; i++) {
                    subarray[j][i] = myarray[j].split(",");
                }
            }
    
            br.close();
    Last edited by jtothemax; 05-13-2012 at 10:25 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by jtothemax View Post
    Hello.

    I have wrote some code to read atext file line by line.
    As it is reads each line, the string is added to an Array.
    Afterwards, it splits the contents into another Array.

    The only problem is, it doesn't work.
    All the lines are stored into the Array without a problem.

    Can someone please tell me how to fix this :confused: Thanks!

    Java Code:
            String[] myarray = new String[5];
            String[][] subarray= new String[5][10];
            BufferedReader br = new BufferedReader(new FileReader(text file));
            for (int j = 0; j < myarray.length; j++) {
                myarray[j] = br.readLine();
                System.out.println(myarray[j]);
                for (int i = 0; i < myarray.length; i++) {
                    subarray[j][i] = myarray[j].split(",");
                }
            }
    
            br.close();
    What exactly is going on here? Most of the post text and most of the code have been copied verbatim from a thread that's more than 4 years old.
    [SOLVED] Reading a text file into an Array

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    it doesn't work.
    Please explain what is wrong with what the code does now and what you want it to do differently.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by DarrylBurke View Post
    What exactly is going on here? Most of the post text and most of the code have been copied verbatim from a thread that's more than 4 years old.
    [SOLVED] Reading a text file into an Array

    db
    Yeah, I sort of used it as a template

    Quote Originally Posted by Norm View Post
    Please explain what is wrong with what the code does now and what you want it to do differently.
    I want to split each lines' content into another array as each line got like "ba1, asd2, aaw3"
    so I want split the "ba1, asd2, aaw3" into another array which becomes "ba1" , "asd2", "aaw3".
    Last edited by jtothemax; 05-13-2012 at 01:38 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    The String class's split() method will split a String into separate parts and put those parts into an array. You will need a regular expression that defines the delimiters between the parts of the String to use with the split() method.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by Norm View Post
    The String class's split() method will split a String into separate parts and put those parts into an array. You will need a regular expression that defines the delimiters between the parts of the String to use with the split() method.
    Sorry, I don't really understand what you mean. Could you show me an example?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Java Code:
         
          String[] sepStr = "a b c".split(" ");
          System.out.println(Arrays.toString(sepStr)); // [a, b, c]
    Creates an array of String subparts by splitting the String at a space
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by Norm View Post
    Java Code:
         
          String[] sepStr = "a b c".split(" ");
          System.out.println(Arrays.toString(sepStr)); // [a, b, c]
    Creates an array of String subparts by splitting the String at a space
    Java Code:
    subarray[j][i] = myarray[j].split(",");
    I get that part but what doesn't this work?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Please post the full text of any error messages.

    What does not work? What is the definition of subarray?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by Norm View Post
    Please post the full text of any error messages.

    What does not work? What is the definition of subarray?
    "Exception in thread "main" java.lang.NullPointerException"
    subarray is 2d String array

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    What is the definition of subarray? How many dimensions does it have?

    What variable has the null value?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by Norm View Post
    What is the definition of subarray? How many dimensions does it have?

    What variable has the null value?
    subarray is a 2 dimensional String array which splits the myarray's contents into many portions

    As far as I know, there's no null value

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    The split() method returns a one dim array. You can not assign it to an element of an array that is not an array.
    This might work if subarray is a 2D String array:
    subarray[i] = aStr.split();
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    jtothemax is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Quote Originally Posted by Norm View Post
    The split() method returns a one dim array. You can not assign it to an element of an array that is not an array.
    This might work if subarray is a 2D String array:
    subarray[i] = aStr.split();
    I tried that. Same error

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Reading a text file into an Array and spliting the content into another Array

    When you read the first line and store it into myarray, you then split it and store it in subarray. Then it attempts to split the second, third etc Strings as well which have not been read yet.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default Re: Reading a text file into an Array and spliting the content into another Array

    Same error
    Please post the code and the error message.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Reading text file into an array
    By Simple_steve in forum New To Java
    Replies: 4
    Last Post: 11-27-2011, 01:45 PM
  2. Reading from a text file into an array
    By xyknight in forum New To Java
    Replies: 16
    Last Post: 04-10-2011, 11:10 PM
  3. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  4. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 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
  •