use matches in string w/o Pattern

Discussion in 'Java' started by rocalp, Feb 18, 2004.

  1. rocalp

    rocalp Guest

    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 ); }
    }
    }
     
    rocalp, Feb 18, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peekachu
    Replies:
    1
    Views:
    337
    Oliver Wong
    Jul 10, 2006
  2. Markus Fischer
    Replies:
    9
    Views:
    165
    7stud --
    Apr 8, 2011
  3. Graham Stow

    Assigning pattern matches to an array

    Graham Stow, Dec 30, 2006, in forum: Perl Misc
    Replies:
    15
    Views:
    168
    Ben Bacarisse
    Jan 1, 2007
  4. jhu
    Replies:
    6
    Views:
    118
    Dave Weaver
    Nov 26, 2007
  5. ChrisC
    Replies:
    4
    Views:
    173
    ChrisC
    Jun 25, 2010
Loading...

Share This Page