Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Sticks_ll is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default textfile into arrays.

    So say I have a text file with lines of text in this structure:

    (stats.txt)
    ALr94/00291/44/LPFP/1341

    Now i want to be able to divide this line, when ever there is a / and add that to an array. Using a Delimiter i guess. So for this line there would be 5 arrays, ALr94 would go into the first array, 00291 would go into the second etc.

    any help or references to text that will help me solve this problem would be most appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    the String.split(...) method is what you want to use. Check out the String API for details.

  3. #3
    Sticks_ll is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    Wow thanks for quick reply. That has got me rolling :D

  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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or else using regular expressions.
    Isn't that kind of like hunting pigeons with an M16 assault rifle? A bit of overkill?

  6. #6
    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

    Not much I think. If you are much cleaver on RegEx it's the easiest way. Anyway over thread starter can mess on this if he/she not familiar with before.

  7. #7
    Sylar's Avatar
    Sylar is offline Member
    Join Date
    Mar 2009
    Location
    Odessa, Texas
    Posts
    16
    Rep Power
    0

    Default

    after reading the File with a LineNumberReader.....

    use a StringTokenizer....via java.util.StringTokenizer...

    StringTokenizer token = new StringTokenizer(<string>,<delimiter>,<boolean include delimiter>)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    simpler than this?:
    Java Code:
          Scanner scanner = new Scanner(new File(STATS_TXT));
          List<String[]> stringArrayList = new ArrayList<String[]>();
          while (scanner.hasNextLine())
          {
            stringArrayList.add(scanner.nextLine().split("/"));
          }
          for (String[] strings : stringArrayList)
          {
            System.out.println(Arrays.toString(strings));
          }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sylar View Post
    after reading the File with a LineNumberReader.....
    use a StringTokenizer....via java.util.StringTokenizer...
    ... or not.

    Per the StringTokenizer API:
    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
    So the folks at Sun are telling us to avoid using StringTokenizer but instead to use either what Eranga or I suggested.

  10. #10
    Sylar's Avatar
    Sylar is offline Member
    Join Date
    Mar 2009
    Location
    Odessa, Texas
    Posts
    16
    Rep Power
    0

    Default

    aw tnx...

    we were not allowed to use scanners in our class...

    i wonder why...

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Scanner is part of Java 1.5 and above. Perhaps you are required to be 1.4 compliant.

  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

    May be...

    Java Code:
            Scanner scanner = new Scanner(new File(STATS_TXT));
            Pattern p = Pattern.compile("[/s]");
            String[] result = null;
    
            while (scanner.hasNextLine()) {
                result = p.split(scanner.nextLine());
            }
            for (int i=0; i<result.length; i++) {
                System.out.println(result[i]);
            }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, that looks decent and simple as my attempt. Thanks!

  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

    Default

    Quote Originally Posted by Fubarable View Post
    Yep, that looks decent and simple as my attempt. Thanks!
    Yes, it's depends on the way we want to process the task. I mean sometime the way you explain is much easier than RegEx. Point is we have lots of ways to do the same task.

  15. #15
    Sticks_ll is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    simpler than this?:
    Java Code:
          Scanner scanner = new Scanner(new File(STATS_TXT));
          List<String[]> stringArrayList = new ArrayList<String[]>();
          while (scanner.hasNextLine())
          {
            stringArrayList.add(scanner.nextLine().split("/"));
          }
          for (String[] strings : stringArrayList)
          {
            System.out.println(Arrays.toString(strings));
          }

    1 sec im confused as to how this code works. Can you just clarify if im getting this processes right.

    while (scanner.hasNextLine())
    {
    stringArrayList.add(scanner.nextLine().split("/"));
    This adds the first value of each line to the first array, 2nd value of each line into the 2nd array etc...?
    It doesn't split all the values of the first line and then put them into the first array does it?

    here is a more detailed explanation. I have a text file with values like this

    asdaf/36532/vsg42/52t
    3415/fwgytyd/353tbd/dh45yg
    3r6eg/e5ge/35gdf/453s

    So I want to be able to put all the first values of each line into the first array, and all the second values of each line in the second array etc..separated by the delimiter /.
    thankyou

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

  17. #17
    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

    Ya, bit of misses there. I just write down it only for a single line of text, not for multiple lines.

  18. #18
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    darn i just del it and yo reply.
    yeah i know you were editing fubarable's code.
    happends to all of us.

    edit: let me add it back in then.
    Java Code:
            Scanner scanner = new Scanner(new File(STATS_TXT));
            Pattern p = Pattern.compile("[/s]");
            String[] result = null;
    
            while (scanner.hasNextLine()) {
                result = p.split(scanner.nextLine());
            [B]// } // moved brace[/B]
                for (int i=0; i<result.length; i++) {
                    System.out.println(result[i]);
                }
            [B]} // added brace[/B]
    ISP messing up, i'll take my leave now.
    Last edited by angryboy; 03-11-2009 at 05:18 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  19. #19
    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 angryboy View Post
    yeah i know you were editing fubarable's code.
    I'm not clear what you say here. I didn't edit anyone's post in this thread.

  20. #20
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i didn't say that. i meant in your code, it looked like you added regex to fubarable's code.

    fubarable's code
    Java Code:
          Scanner scanner = new Scanner(new File(STATS_TXT));
          List<String[]> stringArrayList = new ArrayList<String[]>();
           *while loop
           *for loop
    eranga's code
    Java Code:
           Scanner scanner = new Scanner(new File(STATS_TXT));
          [B] Pattern p = Pattern.compile("[/s]");[/B]
           *while loop
           *for loop
    if its not, then my mistake. now lets back to the OP.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Page 1 of 2 12 LastLast

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 04:02 PM
  2. Inserting a Table in a TextFile?
    By Keerti in forum Advanced Java
    Replies: 5
    Last Post: 11-19-2008, 04:31 PM
  3. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 11:03 AM
  4. textfile with Java problems
    By saytri in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:16 PM
  5. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 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
  •