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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top