use matches in string w/o Pattern

R

rocalp

I have a problem using matches in string w/o using pattern. In the
following code, st.nextToken() returns "111-11-1111" to "s". s.matches
returns "true" to boolean "bl" and "s" still has the string value. But
after if (bl), the value of "bl" and "s" are empty, and it is always
stepping into System.out.println(s) no matter "bl" is true or false. I
don't know why after executing "if (bl)", the values of "bl" and "s"
are cleaned up. I also tried "while (bl)" and the result is the same.
Do I have to use Pattern/matches?

****************************************************************

import java.io.*;
import java.util.*;

public class stuRec {
public static void main(String f[]) {
int i=0;
char c;
try{
FileReader fr = new FileReader("student.txt");
BufferedReader br = new BufferedReader(fr);
StringTokenizer st;
System.out.flush();
String myline = br.readLine();
st = new StringTokenizer(myline);
while (st.hasMoreTokens()) {
String s = st.nextToken();
boolean bl =
s.matches("\\d\\d\\d-\\d\\d-\\d\\d\\d\\d");
if (bl){
System.out.println(s);
}

}

} catch (IOException ioe) {
System.out.println("IO error:" + ioe ); }
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top