{"id":1307,"date":"2016-04-29T08:29:27","date_gmt":"2016-04-29T08:29:27","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1307"},"modified":"2016-04-29T08:29:27","modified_gmt":"2016-04-29T08:29:27","slug":"string-tokenizer","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/string-tokenizer.html","title":{"rendered":"String Tokenizer in JAVA with example"},"content":{"rendered":"<p>String Tokenizer class is for breaking a string into tokens. This class is basically a legacy class although it is used only because of its compatible reasons, but its use is discouraged in new code, as its methods does not able to distinguish between identifiers, numbers and strings that are quoted.<\/p>\n<p>Moreover this class method does not able to recognize the comments.<\/p>\n<p>String Tokenizer class implements Enumeration Interface and Extends Object Class.<\/p>\n<hr \/>\n<h4><strong>String Tokenizer Constructors:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. StringTokenizer(String str)<\/strong><\/span><\/p>\n<p>This constructor creates a stringTokenizer \u00a0with the specified string as mentioned in the argument list.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. StringTokenizer(String str , String delim)<\/strong><\/span><\/p>\n<p>This constructor creates a stringTokenizer with the specified string and delimeter\u00a0as given in the argument list.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. StringTokenizer(String str , String delim , boolean returnValue)<\/strong><\/span><\/p>\n<p>This constructor creates a stringTokenizer with the specified string and delimeter\u00a0as mentioned in the argument list. It has a Boolean value returnValue, if it is true delimeter characters are considered to be tokens or else if it is false delimeter characters serve to separate tokens<\/p>\n<hr \/>\n<h4><strong>String Tokenizer Methods<\/strong><strong>\u00a0<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. int countTokens()<\/strong><\/span><\/p>\n<p>This method returns the total number of tokens , as shown in the following program.<\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome to AbhiAndroid\");\r\n\r\nSystem.<em>out<\/em>.println(\"Total Tokens: \" + stringTokenizer.countTokens());\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Total Tokens: 3\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. boolean <\/strong><strong>hasMoreElements<\/strong><strong>()<\/strong><\/span><\/p>\n<p>This methods checks if there are more elements present or not. It returns true if there are, else returns false, as shown in the following program.<\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\n\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome to AbhiAndroid\");\r\n\r\nwhile(stringTokenizer.hasMoreElements()){\r\n\r\nSystem.<em>out<\/em>.println(stringTokenizer.nextElement());\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Welcome\r\n\r\nto\r\n\r\nAbhiAndroid\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. boolean hasMoreTokens()<\/strong><\/span><\/p>\n<p>This methods checks if there are more tokens present or not. It returns true if there are, else returns false, as shown in the following program.<strong>\u00a0<\/strong><\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\n\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome to AbhiAndroid\");\r\n\r\nwhile(stringTokenizer.hasMoreTokens()){\r\nSystem.<em>out<\/em>.println(stringTokenizer.nextToken());\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Welcome to AbhiAndroid\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. Object nextElement()<\/strong><\/span><\/p>\n<p>This method returns the next element from the StringTokenizer Object, as shown in the following program.<\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\n\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome to AbhiAndroid\");\r\n\r\nwhile(stringTokenizer.hasMoreElements()){\r\nSystem.<em>out<\/em>.println(stringTokenizer.nextElement());\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>\u00a0<\/strong><strong>Output:<\/strong><\/span><\/p>\n<pre>Welcome\r\n\r\nto\r\n\r\nAbhiAndroid\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. String nextToken()<\/strong><\/span><\/p>\n<p>This method returns the next token from the StringTokenizer Object, as shown in the following program.<\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome to AbhiAndroid\");\r\n\r\nwhile(stringTokenizer.hasMoreTokens()){\r\nSystem.<em>out<\/em>.println(stringTokenizer.nextToken());\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>\u00a0<\/strong><strong>Output:<\/strong><\/span><\/p>\n<pre>Welcome\r\n\r\nto\r\n\r\nAbhiAndroid\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. String nextToken(String delim)<\/strong><\/span><\/p>\n<p>This method returns the next token from the StringTokenizer Object based on the delimeter as given in the argument list, as shown in the following program.<\/p>\n<pre>import java.util.StringTokenizer;\r\npublic class StringTokenizerMethods{\r\npublic static void main(String args[]){\r\n\r\nStringTokenizer stringTokenizer = new StringTokenizer(\"Welcome-to-AbhiAndroid\");\r\n\r\nwhile(stringTokenizer.hasMoreTokens()){\r\nSystem.<em>out<\/em>.println(stringTokenizer.nextToken(\"-\"));\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Welcome\r\n\r\nto\r\n\r\nAbhiAndroid\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>String Tokenizer class is for breaking a string into tokens. This class is basically a legacy class although it is used only because of its compatible reasons, but its use is discouraged in new code, as its methods does not able to distinguish between identifiers, numbers and strings that are quoted. Moreover this class method &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/string-tokenizer.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">String Tokenizer in JAVA with example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1307","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>String Tokenizer In JAVA With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"String Tokenizer class is for breaking a string into tokens. This class is basically a legacy class although it is used only because of its compatible reasons, but its use is discouraged in new code, as its methods does not able to distinguish between identifiers, numbers and strings that are quoted.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/string-tokenizer.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1307"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1307\/revisions"}],"predecessor-version":[{"id":1451,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1307\/revisions\/1451"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}