182k views
3 votes
What will be the tokens in the following statement? StringTokenizer strToken = new StringTokenizer("January 1, 2008", ", ", true);

User Sharku
by
4.2k points

1 Answer

5 votes

Answer:

The tokens are:

January

space

1

,

space

2008

Step-by-step explanation:

StringTokenizer class is basically used to break a string into tokens. The string consists of numbers, letters, quotation marks commas etc,

For example if the string is "My name is ABC" then StringTokenizer will break this string to following tokens:

My

name

is

ABC

So in the above example, the string is "January 1, 2008". It contains January with a space, then 1, then a comma, then 2008

The last comma and true in the statement do not belong to the string. They are the parameters of the StringTokenizer constructor i.e.

StringTokenizer(String str, String delim, boolean flag)

So according to the above constructor str= "January 1, 2008"

String delim is ,

boolean flag = true

So the StringTokenizer strToken = new StringTokenizer("January 1, 2008", ", ", true); statement has:

a string January 1, 2008

a delim comma with a space (, ) which is used to tokenize the string "January 1, 2008".

boolean flag set to true which means the delim i.e. (, ) is also considered to be a token in the string. This means the comma and space are considered to be tokens in the given string January 1, 2008.

Hence the output is January space 1 comma space 2008.

Check the attached image to see the output of the following statement.

What will be the tokens in the following statement? StringTokenizer strToken = new-example-1
User Katherine Williams
by
3.6k points