Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stringtokenizer problem

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
0
0
IN
hi i am testing a code

Code:
import java.io.*;
import java.util.*;

class TestProgram
	
{
	public static void main(String args[])

	{         
	   StringTokenizer st = new  StringTokenizer(" this is a test ");

	 
	  String n = st.nextToken();	

          System.out.println("n="+n);

	}
}
>>java TestProgram

n=this

this is giving me first token "this"....but say i want to get third token ONLY (in this example code it is "a")...how can i get that??????

in fact i want to extract any desired token from a string...what is the procedure???
 
Shortly: by using string tokenizer you can not do it.But you can use regular expressions for that reasons. See package:
java.util.regex

Ion Filipski
1c.bmp
 
...../docs/api/java/util/regex/package-summary.html

Ion Filipski
1c.bmp
 
ok...but i did not wanted to mean pattern matching....

i found an example in
based upon that i wrote

Code:
import java.io.*;

import java.util.regex.*;
class TestProgram
	
{
	public static void main(String args[])

	{         
	String pattern = "Now";
        String text = "Now is the time";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(text);
        while (m.find()) 
		
	{
            System.out.print(text.substring(m.start(), m.end()) );
        }
   
}	

}

indeed ,in this code certainly pattern "Now" is identified and being extracted.....

but...

in my original post,
actually i was thinking that if i could do st++ (something like pointer in C ) to traverse my all tokens and could pick up the tokens one by one ,....probaily this is not possible in java...or is it possible in someother way.?????


by the way, about the above regular expression code

look,
Code:
Pattern p = Pattern.compile(pattern);

its peculiar type of syntax to me.....there is no "new" keyword !!!!!!!its creating an object!!!
can you tell me how that syntax works????
 
could you be more explicitly? Do you want to iterate tokens one by one? Do like this

while(st.hasMoreTokens())
x = st.nextToken();


Ion Filipski
1c.bmp
 
Code:
hi i am testing a code



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

class TestProgram {
    public static void main(String args[]) {         
       StringTokenizer st = new  StringTokenizer("this is a test", " ");
     int count = st.countTokens();
     int current = 1;
      while (st.hasMoreTokens()) {
        String n = st.nextToken();
        if (count == current) {    
            System.out.println("token on count = "  +count + " is " +n);
        } else {
            System.out.println("other tokens are " +n);
        }
        count++;
      }
    }
}

BTW,
Code:
Pattern p = Pattern.compile(pattern);
works because the Pattern.compile(pattern) is a "static" method of the class Pattern - ie there is no need for the "new" keyword.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top