Reputation: 21281
I'm trying to read input word by word, but couldn't figure out how to set Scanner's delimiter to whitespace and punctuation marks except ' (the single quote). Here's what I got
BufferedReader input;
String line;
Scanner sc;
String word;
try {
input = new BufferedReader(new FileReader(path));
while (input.ready()) {
line = input.readLine();
System.out.println("Current Line: " + line);
sc = new Scanner(line);
sc.useDelimiter("\\W\\s^\'");
//...
}
}
//...
Upvotes: 2
Views: 2486
Reputation: 195
You can also use the Tokenizer like that:
StringTokenizer st1 = new StringTokenizer("a|b|c");
while(st1.hasMoreTokens())
System.out.println(st1.nextToken());
Hope that could help you in your case.
Upvotes: 1
Reputation: 533502
I assume you mean?
sc.useDelimiter("\\W\\s^\'");
I would use
sc.useDelimiter("[^\\w']+");
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
System.out.println(Arrays.toString(line.split("[^\\w']+")));
prints
[Hello, world, 'Computer, Science', Hell]
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
Scanner scan = new Scanner(line);
scan.useDelimiter("[^\\w']+");
while(scan.hasNext())
System.out.print("|"+scan.next());
System.out.println("|");
prints
|Hello|world|'Computer|Science'|Hell|
Upvotes: 2