Results 1 to 3 of 3
  1. #1
    acp26b is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default Problem with delim in StringTokenizer

    I am having a problem with StringTokenizer, i would like to delim on a string "->" the problem i am having is the strings i am tokenizing sometimes have a '-' or a '>' in them, and when ever it finds either it counts it as a token, is there a way to force it to use a string to delim or am i going to have to write my own? I have written an example below, any help would be much appreciated.


    Code:
    Java Code:
    import java.util.*;
    class testtoken{
    	public static void main (String [] args){
    		int i;
    		String [] testtoken = new String[4];
    		testtoken[0] = "Test->Token->Test"; 
    		testtoken[1] = "Test->To-ken->Test";
    		testtoken[2] = "Test->To>ken->Test";
    		testtoken[3] = "Test->To->ken->Test";
    						
    		for(i = 0; i < testtoken.length; i++){
    			System.out.println("String: " + testtoken[i]);
    			StringTokenizer st = new StringTokenizer(testtoken[i],"->");
    			
    			while(st.hasMoreTokens()){
    				System.out.println("\t" + st.nextToken());
    			}
    		}
    	}
    }

    Output:
    Java Code:
    String: Test->Token->Test
            Test
            Token
            Test
    String: Test->To-ken->Test
            Test
            To
            ken
            Test
    String: Test->To>ken->Test
            Test
            To
            ken
            Test
    String: Test->To->ken->Test
            Test
            To
            ken
            Test
    Desired Output:

    Java Code:
    String: Test->Token->Test
            Test
            Token
            Test
    String: Test->To-ken->Test
            Test
            Token
            Test
    String: Test->To>ken->Test
            Test
            Token
            Test
    String: Test->To->ken->Test
            Test
            To
            ken
            Test

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Dealing with different delimiters with StringTokenizer is a bit fiddly. StringTokenizer has largely been superseded by the String.split() method, and the regular expressions API in general. Try the following:

    Java Code:
    String[] toks = str.split("\\b(-|>|->)\\b");
    In the pattern you pass into split():
    \\b means a "word boundary"
    | means an "alternative"

    To find out more, read about Java regular expressions (link is to one of my tutorials; see also the documentation of the Pattern class).

  3. #3
    acp26b is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    Thanks Neil, it is now working perfectly!


    Java Code:
    import java.util.*;
    class token{
    	public static void main (String [] args){
    		int i;
    		String [] testtoken = new String[4];
    		testtoken[0] = "Test->Token->Test"; 
    		testtoken[1] = "Test->To-ken->Test";
    		testtoken[2] = "Test->To>ken->Test";
    		testtoken[3] = "Test->To->ken->Test";
    						
    		for(i = 0; i < testtoken.length; i++){
    			System.out.println("String: " + testtoken[i]);
    					
    			String[] toks = testtoken[i].split("\\b(->)\\b");
    
    			for(int t = 0; t < toks.length; t++){
    				System.out.println("\t" + toks[t]);
    				
    			}
    		}
    	}
    }
    Output:
    Java Code:
    String: Test->Token->Test
            Test
            Token
            Test
    String: Test->To-ken->Test
            Test
            To-ken
            Test
    String: Test->To>ken->Test
            Test
            To>ken
            Test
    String: Test->To->ken->Test
            Test
            To
            ken
            Test

Similar Threads

  1. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 04:09 PM
  2. [SOLVED] Questions about StringTokenizer
    By miss.meli in forum New To Java
    Replies: 2
    Last Post: 12-03-2008, 08:26 AM
  3. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 08:19 PM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •