-
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
-
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.
-
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:
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?
-
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
-
Replies: 5
Last Post: 02-23-2005, 12:13 AM
-
Replies: 0
Last Post: 04-29-2003, 01:10 AM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By newToJava in forum Java
Replies: 2
Last Post: 03-07-2001, 08:25 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks