difficulty using scanner with bar

My program is meant to parse a bar-delimited file, but when the delimiter is set as a bar, the scanner seems to be making each character a token instead of tokenizing at the bar. This does not happen when the delimiter is set as a colon, for a colon-delimited file.

I am reading these two files:

"bar.txt":

100|first|second|third

200|alpha|beta|gamma

300|roy|gee|biv

"colon.txt":

100:first:second:third

200:alpha:beta:gamma

300:roy:gee:biv

package scantext;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

publicclass TheClass{

privatestaticvoid readFile(String filename, String delim){

try{

File file =new File(filename);

Scanner scanner =new Scanner(file);

scanner.useDelimiter(System.getProperty("line.separator"));

while (scanner.hasNext()){

System.out.println();

String theNext = scanner.next();

System.out.println(theNext);

parseline(theNext, delim);

}

scanner.close();

}catch (FileNotFoundException e){

e.printStackTrace();

}

}

publicstaticvoid parseline(String line, String delim){

Scanner lineScanner =new Scanner(line);

lineScanner.useDelimiter(delim);

System.out.println("delimiter is >" + lineScanner.delimiter() +"<");

int a = lineScanner.nextInt();

String b = lineScanner.next();

String c = lineScanner.next();

String d = lineScanner.next();

System.out.println(

"a = " + a +

", b= " + b +

", c = " + c +

", d = " + d);

}

publicstaticvoid main(String[] args){

readFile("bar.txt","|");

System.out.println();

readFile("colon.txt",":");

}

}

this is the output:

100|first|second|third

delimiter is >|<

a = 1, b= 0, c = 0, d = |

200|alpha|beta|gamma

delimiter is >|<

a = 2, b= 0, c = 0, d = |

300|roy|gee|biv

delimiter is >|<

a = 3, b= 0, c = 0, d = |

100:first:second:third

delimiter is >:<

a = 100, b= first, c = second, d = third

200:alpha:beta:gamma

delimiter is >:<

a = 200, b= alpha, c = beta, d = gamma

300:roy:gee:biv

delimiter is >:<

a = 300, b= roy, c = gee, d = biv

Message was edited by:

dough-re-mi

[3821 byte] By [dough-re-mia] at [2007-11-26 16:20:57]
# 1

Because of the fact the the | is a special character, and needs to be escaped to be used in a regex, which is what the scanner is doing, you need to set the delimiter as "\\|"

public static void main(String[] args) throws IOException {

readFile("bar.txt", "\\|");

System.out.println();

readFile("colon.txt", ":");

}

//output

100|first|second|third

delimiter is >\|<

a = 100, b= first, c = second, d = third

200|alpha|beta|gamma

delimiter is >\|<

a = 200, b= alpha, c = beta, d = gamma

300|roy|gee|biv

delimiter is >\|<

a = 300, b= roy, c = gee, d = biv

100:first:second:third

delimiter is >:<

a = 100, b= first, c = second, d = third

200:alpha:beta:gamma

delimiter is >:<

a = 200, b= alpha, c = beta, d = gamma

300:roy:gee:biv

delimiter is >:<

a = 300, b= roy, c = gee, d = biv

~Tim

Message was edited by:

SomeoneElse

Added code and output

SomeoneElsea at 2007-7-8 22:44:34 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks. I had tried using "\|", but did not think to add the second slash.
dough-re-mia at 2007-7-8 22:44:34 > top of Java-index,Java Essentials,Java Programming...