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 ); }
}
}
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 ); }
}
}