Name: rmT116609 Date: 03/04/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Windows 98 [Version 4.10.2222]
DESCRIPTION OF THE PROBLEM :
The Matcher.appendReplacement example is wrong. The while-loop is illegal.
The problem with the example is that the syntax for a 'while' loop is:
while(<boolean expression>) <statement>
but in the API example, the statement is
while(m.appendReplacement(sb, "dog"))
Matcher.appendReplacement(StringBuffer,String) returns an object of type Matcher, which is not a boolean.
Running the example gave the following error:
---------------------------------------------------------------
C:\TEMP\Pattest.java:11: incompatible types
found : java.util.regex.Matcher
required: boolean
while (m.appendReplacement(sb, "dog")) {
^
1 error
Process completed with exit code 1
----------------------------------------------------------------
Note the "required: boolean" comment.
A complete program that contains the correctly written example is:
------------------------------------------------------------------
import java.util.regex.*;
public class Pattest {
public static void main(String[] args) {
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "dog");
result = m.find();
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
--------------------------------------------------------------------
(Review ID: 143669)
======================================================================
- duplicates
-
JDK-4652835 Javadoc incorrect for java.util.regex.Matcher.appendReplacement
-
- Resolved
-