Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default scanning a file and using more than one delimiter

    Hi Guys! I'm trying to scan a file which uses a few different delimiters to separate different types of data. Here is how the data is formatted

    artist#album
    track name/2.44#track name/2.44#track name/2.44#
    track name/2.44#track name/2.44#track name/2.44#track name/2.44#
    track name/2.44#track name/2.44#track name/2.44#
    track name/2.44#track name/2.44#track name/2.44#track name/2.44#track name/2.44%
    artist#album
    track name/2.44#track name/2.44#track name/2.44#
    track name/2.44#track name/2.44#track name/2.44#
    track name/2.44#track name/2.44#
    track name/2.44
    There are three delimiters. The # splits each bit of information. / is used to split a track name and its length and % signifies the end of an album. There is no % when there isn't another album.

    I can scan the first line, and separate it using # as the delimiter. I can also print out the lines with the track names and length, but just as they are displayed on the line (eg. track name/2.44#track name/2.44#track name/2.44#).

    So far I have this:



    Running this would return along the lines of:



    What do I need to do to be able to pick out an entire album (eg, from nothing to %, % to %, or, % to nothing).

    Split up the data so the stuff before the first # is the artist, then between that and the next # is the album, the the rest of the information between # until % is reached are tracks and track times. Then how can I split the track time and track length up.

    I'll ultimately stick this data into an array and loop around each time to get all the data.
    Last edited by thomase; 03-18-2009 at 03:00 PM. Reason: removed code for privacy of project

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

    Default

    Why not first scan the file using "\\%" as the scanner delimiter. Each iteration of this will give you a String that holds the information for each album. Then in the while loop where you read an album string, read this String line by line and split(...) it with proper delimiters, and place the extracted info into your classes.

  3. #3
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why not first scan the file using "\\%" as the scanner delimiter. Each iteration of this will give you a String that holds the information for each album. Then in the while loop where you read an album string, read this String line by line and split(...) it with proper delimiters, and place the extracted info into your classes.
    Sounds like a good plan. I wanted to split it off album first, but I could only work out how to scan each line, rather than just a block of text for the %.

    How do I modify what I've got to scan a block, not just each line? I've been trying next() but that doesn't really do a whole lot (or doesn't how I use it :P).

    Does "\\%" mean between %?

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

    Default

    A Scanner's delimiter String is a regex String. Because of that, if you use % as a delimiter, you must escape it first with \\ because the non-escaped % has meaning in the regex lexicon.

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

    Default

    I did it, and it works, but it's uuuugly code.

  6. #6
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    So did you just change this line in my code submitted to begin with?

    Java Code:
            Scanner delimitedData = new Scanner(dataSet).useDelimiter("\\%");

    When I run it with that changed I get the following error:

    init:
    run-single:
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at albumlist.AlbumListDriver.main(AlbumListDriver.jav a:34)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

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

    Default

    I did use that delimiter, and I ran into that problem as well. I used a lot of println statements to debug it, and what I think was happening was that the scanner would grab the first String (everything up to the '%' character), but did not grab the end of line character after it. This I had to do manually.

    A way to get around this is to use % + any whitespace that may be present as the delimiter between albums, something like so: fileScanner.useDelimiter("\\%\\s*");
    Last edited by Fubarable; 03-17-2009 at 12:50 PM.

  8. #8
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    A way to get around this is to use % + any whitespace that may be present as the delimiter between albums, something like so: fileScanner.useDelimiter("\\%\\s*");

    Even with that delimiter I still get this error.

    run-single:
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at albumlist.AlbumListDriver.main(AlbumListDriver.jav a:34)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Is it because the scanner is only reading either the next/next line. How can I get it just to read the whole lot then split it properly with %?

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

    Default

    I need to see your code to see what you are doing wrong.

  10. #10
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    here you go :)

    Last edited by thomase; 03-18-2009 at 03:00 PM. Reason: removed code for privacy of project

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

    Default

    Think on this. If you are using % to separate out each album from each other, are you going to want to use it on lines that have been read in, or on the whole file? If you get nextLine, will your scanner even use the delimiter?

    You're also missing some loops here.
    Last edited by Fubarable; 03-17-2009 at 01:32 PM.

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

    Default

    for instance:
    Java Code:
    import java.util.Scanner;
    
    /**
     * @link: http://www.java-forums.org/new-java/17091-scanning-file-using-more-than-one-delimiter.html
     * @author thomase
     *
     */
    public class AlbumDriverFubar0b
    {
    
      private static final String STREAM_NAME = "AlbumData.txt";
    
      public static void main(String[] args)
      {
        Scanner fileScan = null;
        String dataSet = null;
        try
        {
          // I need to make these changes so that my Scanner can find the data:
          //fileScan = new Scanner(new File("AlbumInput"));
          fileScan = new Scanner(AlbumDriverFubar0b.class.getResourceAsStream(STREAM_NAME));
          
          // apply the delimiter to the scanner reading the whole file 
          fileScan.useDelimiter("\\%\\s*");
          while (fileScan.hasNext())
          {
            dataSet = fileScan.next();
            
            // see if it works
            System.out.println("dataSet = " + dataSet);
            
            // if it does, now do something with dataSet
          }
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    }

  13. #13
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Oh. Got it working now! I though I needed a while when you mentioned looping. I've added that where shown.

    I've now got it to split data into

    artist
    album
    track/length
    track/length
    track/length

    until it runs out of information for that section which is good :). Thanks for the help..tried repping again but it wouldn't let me :(. Sure I'll be posting again later when something else isn't going well! :)

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

    Default

    You're welcome. Doesn't it feel amazing when it finally all comes together and works like you want it to? That's what addicts most of us to programming, I think.

  15. #15
    mipravin is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default about running servlets in tomcat

    i am getting http status 500 and http status 404 error when running servlets..how can i solve this? for same program i am getting alternating error that is http status 500 and http ststus 404...plz help me...

  16. #16
    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 mipravin View Post
    i am getting http status 500 ...
    Wouldn't you rather start your own new thread for this question rather than hijack someone else's?

  17. #17
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You're welcome. Doesn't it feel amazing when it finally all comes together and works like you want it to? That's what addicts most of us to programming, I think.
    Yep, just to begin with need a push to try and understand some things and a motivation just to keep on going. I do enjoy it when it goes well!

    Quote Originally Posted by mipravin View Post
    i am getting http status 500 and http status 404 error when running servlets..how can i solve this? for same program i am getting alternating error that is http status 500 and http ststus 404...plz help me...
    Maybe start a new thread rather than use this one? This isn't on servlets so won't be much use. Have you tried rebuilding your project? From memory that helped solve some of my 404/500 errors...but it was a few months ago when I used them.

  18. #18
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default Similar case

    I have a similar problem.

    Im trying to read in several lines

    ex:
    9#H#Hockey Player 5#1#Red Wings#Goalie#32#600000#0

    I need to split them up into 9 different variables to store in a specific class. But i am stumped on what to do.

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

    Default

    String method split(...) is what will work here.

    But as noted above, you should not hijack someone else's thread. Start your own thread for your own question.

  20. #20
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    I am trying split() but i have 3 different class that inherit from a super class, each is a different sport with different some different params

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 02-20-2009, 02:06 PM
  2. Use of Scanner class and Delimiter
    By tjhodge in forum New To Java
    Replies: 3
    Last Post: 02-12-2009, 05:26 PM
  3. Java Twain Scanning
    By mwalstra in forum Advanced Java
    Replies: 3
    Last Post: 12-09-2008, 11:49 AM
  4. using Delimiter with metacharacters
    By wntdaliv in forum New To Java
    Replies: 10
    Last Post: 12-02-2008, 06:42 AM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 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
  •