File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer

 
francis varkey
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a string , String str="~~20";.
Can I tokenize this string using StringTokenizer or using spilt() method String class. I need the out put like.

String id="", String name="", String age=20;

Thanks in advance
Francis
 
bhagya hari
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use stringTokenizer if you use the "~" as separator for the id and name.
 
Manoj Maniraj
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java API recommends not to use StringTokenizer any more

There are empty strings are indicated by the ~ with nothing between them that's a lot more work with a StringTokenizer. By default it gives you just "20", and skips the empties. You can use a special constructor that takes a boolean to tell the StringTokenizer to return delimiters, but that gets complicated too.

It's much easier to use split("~"), which immediately returns {"", "", "20"}


Cheers..
 
francis varkey
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"~" is my delimiter.
 
Manoj Maniraj
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
francis varkey
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
String id="";
String name="";
String age="";
String str="~~20";
try{
StringTokenizer stkn=new StringTokenizer(str,"~");
id=stkn.nextToken();
name=stkn.nextToken();
age=stkn.nextToken();

System.out.println(id);
System.out.println(name);
System.out.println(age);

}
catch(Exception ex){
ex.printStackTrace();
}

this will give exception

2)

String id="";
String name="";
String age="";
String str="101~~";
try{
String ar[]=str.split("~");
id=ar[0];
name=ar[1];
age=ar[2];

System.out.println(id);
System.out.println(name);
System.out.println(age);

}
catch(Exception ex){
ex.printStackTrace();
}

this will also give the exception.

How to handle such situations ?



 
Piet Verdriet
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
francis varkey wrote: ...

this will also give the exception.

How to handle such situations ?



Use:

 
francis varkey
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You. It is working fine.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
francis varkey wrote:Thank You. It is working fine.


You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic