Results 1 to 14 of 14
  1. #1
    venkat.ravala is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default How to write text file into Array

    Hi i am venkatramana. I am new to java. I am doing small task. I am reading text file(numbers) line by line and storing that lines into one array but after pushing into array i am printing the array values to test. but it is not printing values it is printing NULL values instead of numbers. I think it is not I am sending my code .Please can u any body help.

    import java.io.*;
    public class Dataread {


    public static void main(String[] args) throws Exception {



    FileInputStream fstream = new FileInputStream("C:\\TestFile.txt");

    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    int i=0;
    String anArray[];

    while((strLine = br.readLine())!= null)
    {
    i++;
    System.out.println(i);


    }
    anArray = new String[i];


    for (int j = 0; j<i; j++)
    {
    anArray[j] = br.readLine();
    //anArray[j] = String.arseString(br.readLine());

    System.out.println(anArray[j]);// It is printing the NULL values.

    }

    in.close();
    }
    }

    Can any body help pleae

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    In your first while loop you're reading the entire file (line by line) to figure out the number of lines. Next you build an array given the number of lines and at the end you're trying to read the entire file again but it was at its end of file position already so the readLine() method returns null over and over again.

    After your first while loop close your file and re-open it again if you want to re-read every line again. btw, why are you using a DataInputStream?

    kind regards,

    Jos

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    The better solution is to not use an Array. Instead use an ArrayList. Then as you read each line of data you simply add the line to the ArrayList. No need to read the data in the file twice.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also ArrayLists are useful because of the followings.

    1. Implements all methods from the List interface
    2. Permits null elements
    3. Only the Objects can be added to an ArrayList
    4. The isEmpty, size, iterator, set, get and listIterator operations


  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    ..
    3. Only the Objects can be added to an ArrayList
    ..
    Autoboxing allows adding of primitives without explicitly converting to the wrapper type.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Great spoon feeding! Make sure the OP doesn't learn a thing.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by merehephet View Post
    You are entitled to your opinion
    I second PhHein's opinion.

    kind regards,

    Jos

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by merehephet View Post
    Well, sarcasm and aloofness don't feature very highly in any pedagogical theories I have ever studied. For more on helping people to learn read about Vygotsky's Zone of Proximal Development.

    best regards.
    Clearly you have no experience on how forums work.
    You are not being clever by trying to correct those who do
    have experience with forums too.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by merehephet View Post
    Absolutely correct. I joined your forum today..
    It's not my forum (Thank God).

    Quote Originally Posted by merehephet View Post
    ...Perhaps I have unwittinglty broken forum protocol, ...
    You can read the forum guidelines if you are not sure.

    Quote Originally Posted by merehephet View Post
    ..but I feel that it doesn't warrant the attack which is now escalating. ...
    You have not been attacked at all. In fact I would go further and say that it's you who has attacked people here. You don't want to know what a real attack from forum regulars is like.

    Quote Originally Posted by merehephet View Post
    .. So can we put this down now and get on with the reasons we are on this site in the first place.
    That's what everyone was doing before you disrupted that by spoon feeding. Didn't it seem strange to you that no one had supplied the full solution when it is obvious that all who had replied knew the answer?

  11. #11
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    You are right. I was quick to be defensive and I apologise unreservedly to you, JosAH and PhHein and ask if I can start again. I will be more conscientious in the future.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Autoboxing allows adding of primitives without explicitly converting to the wrapper type.
    Of course. I just misses it. Thanks for the comment here, completed answer for our OP.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by merehephet View Post
    You are right. I was quick to be defensive and I apologise unreservedly to you, JosAH and PhHein and ask if I can start again. I will be more conscientious in the future.

    Hi merehephet, please don't delete your posts unless someone is not commented on the post. I've seen that you've delete many posts. If something is going wrong in your post you can edit it. And if you want to delete it permanently from the forum, then please contact one of our moderators, we can remove it for you.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 10
    Last Post: 05-24-2010, 08:25 PM
  2. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 08:48 AM
  3. Replies: 3
    Last Post: 12-12-2008, 12:12 PM
  4. Write a List into a Text file
    By tech2000 in forum New To Java
    Replies: 1
    Last Post: 11-13-2008, 05:09 AM
  5. how do i write to a text file from an arraylist?
    By otoro_java in forum New To Java
    Replies: 3
    Last Post: 01-30-2008, 06:53 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
  •