File i/o - using scanner to parse


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: File i/o - using scanner to parse

  1. #1
    Join Date
    Feb 2006
    Posts
    11

    File i/o - using scanner to parse

    I'm playing around with scanner to see if it will meet my needs for inputting a file and parsing it.

    I've tried the sample in the java tutorial to break a file up with a space as a delimiter.
    However, my file uses an * as the field delimiter.
    Tried this:
    Code:
      
    public static void main(String[] args) throws IOException {
            Scanner s = 
                new Scanner(new BufferedReader(new FileReader(args[0])));
            s.useDelimiter("*");
    
            while (s.hasNext()) {
                System.out.println(s.next());
            }
            s.close();
    }
    but running it gives me the following errors:

    Code:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta
     character '*' near index 0
    *
    ^
    I tried \*, to try to escape it, and that didn't cut it either.
    How can I create an escape sequence or regex for this?

    (Yes, I know I could read the file in using something else, and tokenize it, but I'm exploring my options here.)

    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    use the split method for this. example

    string[] strDel;

    strDel[] = TheString.split("*");

    the TheString is a string variable which contains the data u wan to split, when it splits it, it gets stored in the string array StrDel variable, you can then use a loop to, check the contents of the array such as

    for (int i=0; i<= TheString.lenght;i++)
    {
    System.out.println(strDel[i]);
    }

    Hope that helps.

  3. #3
    Join Date
    Feb 2006
    Posts
    11
    Thanks, but that doesn't quite accomplish what I'm trying to do in this case.

    You're method will work if I read each line in seperately, then parse it. I'm trying to save a step.

    also, I can not make the assumption that each line in the input file ends in a \n. There is a delimiter character defined in the input file, but I'll worry about reading that later. (which is one of the reason's I'm trying this method instead of reading each line and splitting it.)

    For example, here is a sample input:
    Code:
    a*b*c**a b
    c*d
    Expected output:
    Code:
    a
    b
    c
    <no value>
    a bc
    (note the \n is ignored)
    Do you think I need to use the two step approach?

  4. #4
    Join Date
    Feb 2006
    Posts
    11

    Don't program too early

    Doh! It's too early on a Sunday.
    My problem is I wasn't properly escaping the character. (Guess being new to java, I didn't know that I need to escape the escape character.)

    If I use s.useDelimiter("\\*"), it works fine. (Except it also splits out the newline, but I can work around that.)

Similar Threads

  1. Vector and StreamTokenizer problem
    By dagma20 in forum Java
    Replies: 5
    Last Post: 02-23-2005, 12:13 AM
  2. Tomcat FILE I/O
    By Souren in forum Java
    Replies: 0
    Last Post: 04-29-2003, 01:10 AM
  3. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  4. Replies: 2
    Last Post: 03-07-2001, 08:25 PM
  5. File I/O ?????
    By Gary Nelson in forum .NET
    Replies: 32
    Last Post: 03-06-2001, 12:56 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center