Java StringTokenizer example
In Java, you can StringTokennizer class to split a String into different tokenas by defined delimiter.(space is the default delimiter). Here’re two StringTokennizer examples :
Example 1
Uses StringTokennizer to split a string by “space” and “comma” delimiter, and iterate the StringTokenizer elements and print it out one by one.
package com.mkyong; import java.util.StringTokenizer; public class App { public static void main(String[] args) { String str = "This is String , split by StringTokenizer, created by mkyong"; StringTokenizer st = new StringTokenizer(str); System.out.println("---- Split by space ------"); while (st.hasMoreElements()) { System.out.println(st.nextElement()); } System.out.println("---- Split by comma ',' ------"); StringTokenizer st2 = new StringTokenizer(str, ","); while (st2.hasMoreElements()) { System.out.println(st2.nextElement()); } } }
Output
---- Split by space ------ This is String , split by StringTokenizer, created by mkyong ---- Split by comma ',' ------ This is String split by StringTokenizer created by mkyong
Example 2
Read a csv file and use StringTokenizer to split the string by “|” delimiter, and print it out.
File : c:/test.csv
1| 3.29| mkyong 2| 4.345| eclipse
package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class ReadFile { public static void main(String[] args) { BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("c:/test.csv")); while ((line = br.readLine()) != null) { System.out.println(line); StringTokenizer stringTokenizer = new StringTokenizer(line, "|"); while (stringTokenizer.hasMoreElements()) { Integer id = Integer.parseInt(stringTokenizer.nextElement().toString()); Double price = Double.parseDouble(stringTokenizer.nextElement().toString()); String username = stringTokenizer.nextElement().toString(); StringBuilder sb = new StringBuilder(); sb.append("\nId : " + id); sb.append("\nPrice : " + price); sb.append("\nUsername : " + username); sb.append("\n*******************\n"); System.out.println(sb.toString()); } } System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Output
1| 3.29| mkyong Id : 1 Price : 3.29 Username : mkyong ******************* 2| 4.345| eclipse Id : 2 Price : 4.345 Username : eclipse ******************* Done

Thnx alot …
I can’t to get this work using a comma and a space
im not seeing what you are doing
I am getting some exception in this programe…
import java.util.StringTokenizer;
public class App {
public static void main(String[] args) {
String str = “C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\school3″;
StringTokenizer st2 = new StringTokenizer(str, “\”);
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
Try This.. Its working..
import java.util.StringTokenizer;
public class App {
public static void main(String[] args) {
String str = “C:’\'Program Files’\'Apache Software Foundation’\'Tomcat 6.0′\’webapps’\'school3″;
StringTokenizer st2 = new StringTokenizer(str,”‘\’”);
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
I did this is in easiest mode for understanding everyone please try this who are going to learn java tokenizers try this……………
import java.util.StringTokenizer;
class StringTokenizer
{
public static void main(String args[])
{
String pdetails=(“auu=isoluecine;ccu=proline;guu=valine;uuu=phenylalanine”);
StringTokenizer st=new StringTokenizer(pdetails,”;”);
System.out.println(“num of tokens ” +st.countTokens());
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
you’v done an awesome task fella….. thanks…… cheers ! ! !
i m getting a nullPointerException can u plz help
“Exception in thread “main” java.lang.NullPointerException
at java.util.StringTokenizer.(StringTokenizer.java:199)
at java.util.StringTokenizer.(StringTokenizer.java:221)
at token.main(token.java:19)”
my program is:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class token {
public static void main(String[] args)
{
BufferedReader br=null;
try
{
String line;
br= new BufferedReader(new FileReader(“file.txt”));
while((line=br.readLine())!=null)
{
System.out.println(line);
}
StringTokenizer st=new StringTokenizer(line,”");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken(line));
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try
{
if(br!=null)
{
br.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
Check this out..
import java.io.*;
import java.util.StringTokenizer;
public class App {
public static void main(String[] args)
{
BufferedReader br=null;
try
{
String line;
br= new BufferedReader(new FileReader(“/home/mohit/Documents/Java/file.txt”));
while((line=br.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try
{
if(br!=null)
{
br.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
U have to pass the location of the file where ‘file.txt’ is located..
please help me i want to red a text file chrarcter by chararcter and compare each chararcter if is equal print it.
Thanks,
i have read of StringTonkenizer content but some how in ur site it is not completed, it is half on right side of page .
Please make it complete visible sothat people benifited a lot.
Thanks
Sorry, what you means by “half on right side”? I’m able to view the full page.
Try refresh or clear your cache.
Awesome guide. Thanks.
taking string as abc-123 and display cba-321 in java string class
split the string with “-” as token and then reverse it and again append the both reverses string using StringBuilder class.
thank you very much, for your great function!
I’m new in the world of java, but it is like php
It’s useful, but it is suggested to better use split method, reference http://download.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html
It is very useful for me.
Thank u for giving this information.
Your blog is very useful for me. Thanks!
thanks for ur site mr.mkyong! it’s useful to me..
waqai mein bohat alla hai :) Stay blessed or sada suhagan raho :*