How to Read a File in Java Using Stringtokenizer

StringTokenizer is a legacy class for splitting strings into tokens. In order to pause Cord into tokens, you need to create a StringTokenizer object and provide a delimiter for splitting strings into tokens. You tin pass multiple delimiters e.one thousand. yous can break String into tokens by, and: at the aforementioned fourth dimension. If you don't provide any delimiter then by default it will use white-infinite. It'south inferior to split() as it doesn't support regular expression, also it is not very efficient. Since information technology's an obsolete class, don't expect whatever performance comeback either. On the hand split() has gone some major functioning heave on Java 7, encounter here to learn more virtually splitting String with regular expression.

StringTokenizer looks easier to use but you should avoid information technology, except for trivial task. Ever  Prefer String'southward divide() method for splitting String and for repeated split use Pattern.carve up() method.

Coming back to StringTokenizer, nosotros volition see iii examples of StringTokenizer in this commodity. The first example is to suspension String based on white-space, the second instance will show how to utilize multiple delimiters, and the 3rd example volition show you how to count the number of tokens.

In order to go tokens, you basically follow the Enumeration mode model, i.e. checking for more than tokens using hasMoreTokens() and so getting tokens using nextToken().

And, If you are new to the Java earth then I also recommend you go throughThe Consummate Java MasterClasson Udemy to acquire Java in a better and more structured way. This is one of the best and upwards-to-date courses to learn Java online.

Java StringTokenizer Example

StringTokenizer Example in Java with Multiple DelimiterHither is the full code of our Coffee StringTokenizer Example. You lot can copy-paste this code into your favorite IDE and run information technology straight away. Information technology doesn't require any tertiary-party library like Apache commons or Google Guava. All you need to practise is create a Coffee source file with the same name as the public class of this example, the IDE will take care of compiling and running this case.

Alternatively, you can as well compile and execute this case from the control prompt as well. If you look at the get-go example, we take a String where words are separated by a white-infinite, and to get each discussion from that Cord, nosotros have created a StringTokenizer object past passing that String itself, notice we have not provided any delimiter, because by default StringTokenizer uses white-infinite equally a token separator.

In gild to go each token, in our example word, y'all just need to loop, until hasMoreTokens() returns false. Now to become the word itself, but telephone call nextToken() method of StringTokenizer. This is similar to Iterating over Java Collection using the Iterator, where we use the hasNext() method as while loop condition and next() method to become the side by side element from Collection.

The 2d instance is more than interesting because here our text is a spider web address, which has protocol and IP address. Hither we are passing multiple delimiters to split http cord e.one thousand. //( double slash), : (colon) and . (dot), At present StringTokenizer volition create a token if whatsoever of this is establish in target Cord.


The tertiary example shows you how to get a full number of tokens from StringTokenizer, quite useful if you want to copy tokens into an array or drove, as you tin utilise this number to decide the length of array or size of the corresponding drove.

import java.util.StringTokenizer ;

/**

 * Java programme to evidence how to employ StringTokenizer for breaking a delimited

 * Cord into tokens. StringTokenizer allows you to utilise multiple delimiters as

 * well. which means you can split String containing comma and colon in ane call.

 *

 * @writer Javin Paul

 */

public class StringTokenizerDemo {

public static void master (String args[]) {

// Example one - By default StringTokenizer breaks String on space

        System. out . println ( "StringTokenizer Case in Java, split Cord on whitespace" );

        String give-and-take = "Which one is better, StringTokenizer vs Dissever?" ;

        StringTokenizer tokenizer = new StringTokenizer(discussion);

while (tokenizer. hasMoreTokens ()) {

            Organisation. out . println (tokenizer. nextToken ());

        }

// Example 2 - StringTokenizer with multiple delimiter

        System. out . println ( "StringTokenizer multiple delimiter Example in Java" );

        String msg = "http://192.173.15.36:8084/" ;

        StringTokenizer st = new StringTokenizer(msg, "://." );

while (st. hasMoreTokens ()) {

            System. out . println (st. nextToken ());

        }

// Example 3 - Counting number of String tokens

        System. out . println ( "StringTokenizer count Token Instance" );

        String records = "ane,two,three,four,five,six,seven" ;

        StringTokenizer breaker = new StringTokenizer(records, "," );

        System. out . println ( "Total number of tokens : " + breaker. countTokens ());

    }

}

Output:

StringTokenizer Example in Java, divide Cord on whitespace

Which

one

is

better,

StringTokenizer

vs

Split?

StringTokenizer multiple delimiter Example in Java

http

192

173

15

36

8084

StringTokenizer count Token Example

Total number of tokens : 7

Every bit I said, all this functionality is likewise available to the String class' divide method, and yous should use that equally your default tool for creating tokens from String or breaking them based upon whatever limiter. To learn more about thepros and cons of using the StringTokenizer and Split method,  y'all can come across my post difference betwixt Split vs StringTokenizer in Coffee.

That'due south all on how to use StringTokenizer in Java with multiple delimiters. Yeah, it's convenient, especially if you are not very comfortable with regular expression. Past the fashion,  if that's the instance then you better spend some time learning regular expression, non just to carve up String into tokens only to use regex every bit a skill.

You will be surprised to see the power of regular expression, while searching, replacing and doing other text stuff. StringTokenizer is also a legacy class, which is but retained for compatibility reasons and you lot should not apply it in new code.

It is recommended to use the carve up method of String for splitting strings into tokens or Patterns.dissever() method from java.util.regex parcel instead. In terms of performance also, divide() has got a major boost in Java vii from Coffee 6, and information technology's reasonable to expect performance improvement only on thesplit() method, because no work will exist done on StringTokenizer.

babcockwhipmed.blogspot.com

Source: https://javarevisited.blogspot.com/2014/02/stringtokenizer-example-in-java-multiple-delimiters.html

0 Response to "How to Read a File in Java Using Stringtokenizer"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel