Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java array and string question

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java array and string question

    I have a list of test which I want to place into an array. How would I take text from a file, line by line, and then place each line into its own array element? Something like this:
    Bob Hall
    Cliff Jackson
    Casey Waters

    myArray[0] = Bob Hall, myArray[1] = Cliff Jackson, myArray[2] = Casey Waters


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java array and string question

    You'd have to set up a buffered reader then have it collect the data from the file in the line-by-line manner you're wanting and have it in a for loop that counts and just have i as the place in the array and have the line assigned to that place in the array

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Java array and string question

    Quote Originally Posted by colerelm View Post
    I have a list of test which I want to place into an array. How would I take text from a file, line by line, and then place each line into its own array element? Something like this:
    Bob Hall
    Cliff Jackson
    Casey Waters

    myArray[0] = Bob Hall, myArray[1] = Cliff Jackson, myArray[2] = Casey Waters
    Use readLine() of a BufferedReader. It reads until a CR or LF character.
    FileInputStream fstream = new FileInputStream("dir/test.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String str;
    while ((str = br.readLine()) != null) {
    	//add string to myArray
    }

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 10:47 PM
  3. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 04:36 PM