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

Reference

  1. http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Azi
Guest
Azi

Hi mkyong,

What if I like to split both space and comma at the same time.?

Shweta Chaudhary
Guest
Shweta Chaudhary

The java.util.StringTokenizer class allows you to break a string into tokens. stringTokenizer is simple way to break string.

It doesn’t provide the facility to differentiate numbers, quoted strings, identifiers etc. like StreamTokenizer class. We will discuss about the StreamTokenizer class in I/O chapter.

SJ Chon, Esq.
Guest
SJ Chon, Esq.

??? ??? ??? ?????? ??? run? ?????? instance? ??? ??? ???? ????. ? ??? ? ??? ??? ?????

Chaitanya Limaye
Guest
Chaitanya Limaye

Sir,I want to print first letter of each word in string in capital letter…my idea is find the space using charAt method and increment that pointer and apply ToupperCase method to it..but it is not working..what should i do?

Hendrianto
Guest
Hendrianto

Excuis sir i have problem in my thesis. I must created tokenization java code . In journal tokenization said like a variabel,keyword,literal,puntuator and operator.
My question is how i can know tokenization like that in java?
What should i do? Help me sir. Tx

Subhadeep Karan
Guest
Subhadeep Karan

Thanks for the efforts, superb work….:D

Aparna
Guest
Aparna

How to remove stopwords from the tockenized output?

hashim pk
Guest
hashim pk
halo db.collection.find( { user_id: /^bc/ }) Can you give the example program in java. (using append, System.out.println(“Enter The Searching The Elements : “); String s = in.nextLine(); BasicDBObject findObject = new BasicDBObject(); findObject.append(“field:”, “s”); DBCursor findQuery = collection.find(findObject); System.out.println(findObject.append(“field”,s)); if (findQuery.hasNext()) { System.out.println(findQuery.next()); } is not correct,pls clear this program
sivaram
Guest
sivaram

Thank you

RJ
Guest
RJ

how can i get
“550 5.1.1 The email account that you tried to reach does not exist”

from

“550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient’s email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 m9si12310002oep.43 – gsmtp”

in java… Can anyone help me out??

Shubhrajyoti satpathy
Guest
Shubhrajyoti satpathy

“my name is shubhrajyoti ” want like
mY
naMe
Is
shUbhraJyotI

WHAT WILL BE CODE

Tin
Guest
Tin

Hi Sir,

How can I set format it with columns like below

String1 | String2 | String3
Value1 | Value2 | Value3
Valueeeeee | Valueeeeeee | Valueeeee
eeee1 | eeeee2 | eeee3

Thanks!

Tin
Guest
Tin

it should all be align. but will continue to next line the long value just to maintain the alignment of text

Mohd Asad Khan
Guest
Mohd Asad Khan

Thank Sir
Very Helpful!!!

abdd
Guest
abdd

Please fix the class name,

from:
StringTokennizer

to:
StringTokenizer

otto
Guest
otto

how to read column and match that column, so that can print a line

fifa
Guest
fifa

Hi, I’m getting this error when I run the Java code

Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “age”

It is from this line
Integer age = Integer.parseInt(stringTokenizer.nextElement().toString());

Does anyone know what wrong with my code?

Hamdi
Guest
Hamdi

Thnx alot …

J
Guest
J

I can’t to get this work using a comma and a space
im not seeing what you are doing

Sishir
Guest
Sishir

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());
}
}
}

Mohit Tomar
Guest
Mohit Tomar

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());
}
}
}

gangasani mahesh reddy
Guest
gangasani mahesh reddy

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());
}
}
}

Dorababu
Guest
Dorababu

Here in the above example how did you parse auu=isoluecine ….. into a java object

onlineseeker
Guest
onlineseeker

you’v done an awesome task fella….. thanks…… cheers ! ! !

m24
Guest
m24

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();
}
}

}
}

Mohit Tomar
Guest
Mohit Tomar

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();
}
}
}
}

Mohit Tomar
Guest
Mohit Tomar

U have to pass the location of the file where ‘file.txt’ is located..

ahmadi
Guest
ahmadi

please help me i want to red a text file chrarcter by chararcter and compare each chararcter if is equal print it.

Bhavesh tamrakar
Guest
Bhavesh tamrakar

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

trackback
String Tokenizer vs Scanner vs Split « tediscript.wordpress.com
Anonymous
Guest
Anonymous

Awesome guide. Thanks.

Amar
Guest
Amar

taking string as abc-123 and display cba-321 in java string class

swarnim prabhat
Guest
swarnim prabhat

split the string with “-” as token and then reverse it and again append the both reverses string using StringBuilder class.

to split
Guest
to split

thank you very much, for your great function!
I’m new in the world of java, but it is like php

SureshMhz
Guest
SureshMhz

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

selvam
Guest
selvam

It is very useful for me.
Thank u for giving this information.