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 5 of 5

Thread: Splitting File into Array

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Splitting File into Array

    Hi,

    Basically I have a text file that I want to read line by line (I have this bit working) and write it to an array.

    However I want the data from the text file to be split after every character.

    For example:

    Text file line 1: ABCDEFG
    Array: { A, B, C, D, E, F, G}

    Could anyone give me any help on this?

    Thanks
    SJ996


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Splitting File into Array

    If you split a string on an empty string, it will split that string on characters:
    		String sb = "ABCDE";
    		String[] s = sb.split("");
    		for (String st : s ){
    			System.out.println(st);
    		}

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Splitting File into Array

    You can get a character at a certain position in the string using the charAt() method:

    String str = "Hello";
    str.charAt(1); // 'e'

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Splitting File into Array

    Thanks for the replies. this has helped alot.

    Does anyone know how to omit the first two lines from the input text file every time? As i do not want these in the array.

    Scott

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Splitting File into Array

    Use a "dummy read", ie. read them in but don't do anything with that data.
    // skip two lines
    myReader.nextLine();
    myuReader.nextLine();

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 11:44 AM
  2. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 09:48 AM
  3. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM
  4. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  5. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM