Results 1 to 6 of 6

Thread: Reading a token

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Reading a token

    Hello,
    I try to read a sentence like it was a word. Let me explain I suppose that such a file directory:
    TT 10Street cat 00000232
    ...
    ...
    The first word is the name, the last telephone number, but that there's entry represents the customer address. However when I use the method nextToken () of StringTokeniser Obviously he will cut me my chain style.
    Token1: TT
    Token2: 10,
    Token3: Street
    Token4: the
    Token5: cat
    Token6: 00000232
    How can I read the tokens properly, so that I can get the desired output.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Reading a token

    Hello,
    I have a code, please go through the code, I think this will help you.
    package test;
    Public class TokenReader {
    private String[] dic;
    private static Final DictionnaryPattern Pattern = Pattern.compiles("([A-Z] +)\\s +(.+?)\\s + ([0-9] +) ");
    Public TokenReader(String[] d) {
    dic = d;
    }

    Public void analyze() {
    for (String line: dic) {
    Matcher m = dicPattern.matcher(line);
    System.out.System.out.println("Analyzing line:" + Line);
    if (m.games()) {
    System.out.System.out.println("Type" + M.group(1));
    System.out.System.out.println("Address" + M.group(2));
    System.out.System.out.println("Tel:" + M.group(3));
    }
    }
    }
    Public static void hand(String[] args) {
    String[] directory = new String[] {
    "TOTO 10, rue du dog 0000023224"
    ,"TITI 40, boulevard de la victory 3211588522"
    };

    TokenReader r = new TokenReader(Directory);
    stanalyze();
    }
    }

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Reading a token

    Hello,
    But I wanted to know if there is no way to do it with StringTokenizer? Because in my file I have lots of different type of string that I deal with StringTokenizer and nextToken (), there are those types of book that I have a problem. So I did not do a mix between StringTokenizer and the method proposed above. Is there any other way to do this? If any then please do help me.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Reading a token

    Hello,
    Take a look at the following code
    Code:
    public class TokenizingJavaSourceCode{
    
        public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please enter a java fl name: ");
        String flnm = in.readLine();
        if(!flnm.endsWith(".java")){
          System.out.println("This is not a java fl.");
          System.exit(0);
        }
        File javaFile = new File(flnm);
        if(javaFile.exists()){
          FileReader fl = new FileReader(flnm);
          StreamTokenizer streamTokenizer = new StreamTokenizer(fl);
          // It will go through the fl and gives the number of tokens in the fl
          int i=0;
          int nutkgen = 0;
          while(i != StreamTokenizer.TT_EOF){
            i = streamTokenizer.nextToken();
            nutkgen++;
          }
          System.out.println("Number of tokens = " + nutkgen);
        }
        else{
          System.out.println("File does not exist!");
          System.exit(0);
        }
      }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Reading a token

    Hello,
    You can split first and last element, here it is, how you can do it.
    Code:
    String [] table = lntest.split(" ");
    String first = table[0];
    String last = table[board.length-1];
    Try splitting the elements and then see if the code works properly, if not then post the error what you get.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Reading a token

    Hi,
    Here is another example of how you can use the splitting.
    Code:
    StringTokenizer strtok = new StringTokenizer(dbRecord, ":");
    
    String fname = strtok.nextToken();
    String lname = strtok.nextToken();
    String city  = strtok.nextToken();
    String state = strtok.nextToken();
    
    println("First:  " + fname);
    println("Last:   " + lname);
    println("City:        " + city);
    println("State:       " + state);
    Hope this will help you. Best of Luck.

Similar Threads

  1. Determine user object token size
    By Iksura in forum Active Directory
    Replies: 5
    Last Post: 17-05-2012, 11:45 AM
  2. password Token cannot be recognized in skype
    By Omkaari in forum Technology & Internet
    Replies: 2
    Last Post: 15-10-2011, 09:55 PM
  3. Token Gaming for NBA 2K12.
    By AliaShaikh in forum Video Games
    Replies: 3
    Last Post: 06-10-2011, 10:44 AM
  4. Security Token & JW Player 4.4
    By beelow in forum Software Development
    Replies: 3
    Last Post: 08-08-2009, 02:25 PM
  5. passwd: authentication token manipulation error
    By Knopper in forum Operating Systems
    Replies: 3
    Last Post: 29-07-2009, 11:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,031,611.93655 seconds with 17 queries