| |

VerySource

 Forgot password?
 Register
Search
View: 915|Reply: 9

What is the function of this code snippet

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-1 10:00:01
| Show all posts |Read mode
StringTokenizer fenxi = new StringTokenizer (person_email, "@");
            int n = fenxi.countTokens ();
             if (n> = 3)
                 {out.print ("<BR>" + "The email you entered has illegal characters");
                 }
             else
                {putString (person_name);
                  out.print ("<BR>" + "You have successfully registered");
                  out.print ("<BR>" + "Your registered name is" + person_name);
                   
                }

Why if (n> = 3) is not legal, thanks. Why? The separator is not "@"? I do n’t understand, please ask, thanks.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-13 20:00:01
| Show all posts
Determine whether the email address entered by the user is valid,
An email address cannot have more than two @
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-14 09:00:01
| Show all posts
An email address cannot have more than two @
======================================================= ======================
Can there be two?
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-16 16:15:01
| Show all posts
public class StringTokenizerextends Objectimplements Enumeration <Object> The string tokenizer class allows an application to break strings into tokens. The tokenization method is simpler than the methods used by the StreamTokenizer class. The StringTokenizer method does not distinguish between identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

You can specify it at creation time, or you can specify a set of separators (characters that separate the tags) based on each tag.

An instance of StringTokenizer behaves in two ways, depending on whether the value of the returnDelims flag it uses when it is created is true or false:

If the flag is false, the delimiter character is used to separate the tags. A token is the largest sequence of consecutive characters (not delimiters).
If the flag is true, those separator characters are considered tokens themselves. So the token is either a delimiter character or the largest sequence of consecutive characters (not delimiters).
The StringTokenizer object internally maintains the current position to be marked in the string. Some operations move this current position after the processed character.

Returns a token by intercepting a substring of a string that is used to create a StringTokenizer object.

Here is an example using a tokenizer. code show as below:

     StringTokenizer st = new StringTokenizer ("this is a test");
     while (st.hasMoreTokens ()) {
         System.out.println (st.nextToken ());
     }
 The following string is output:

     this
     is
     a
     test
 StringTokenizer is a legacy class that is retained for compatibility reasons (although its use is discouraged in new code). It is recommended that anyone seeking this feature use the split method of String or the java.util.regex package.

The following example illustrates how to use the String.split method to split a string into basic tokens:

     String [] result = "this is a test" .split ("\\s");
     for (int x = 0; x <result.length; x ++)
         System.out.println (result [x]);
 The following string is output:

     this
     is
     a
     test
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-22 22:00:01
| Show all posts
It's quite detailed, I have nothing to say.
Reply

Use magic Report

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

 Author| Post time: 2020-4-27 19:30:02
| Show all posts
Don't COPY DOC document, OK? Brother. .

What I want to ask now is why
Why if (n> = 3) is not legal, thank you. Why is it not separated by "@" ?? Don't understand, please ask, thanks.
Reply

Use magic Report

1

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-1 18:30:01
| Show all posts
countTokens
public int countTokens () counts the number of times this tokenizer's nextToken method can be called before generating an exception. The current position is not advanced.

return:
The number of tokens remaining in the string using the current delimiter set.



Did not understand

The program seems to return incorrect results. Enter a correct Email and prompt an error

import java.util.StringTokenizer;

public class c
{
public static void main (String args []) throws Exception
{
StringTokenizer fenxi = new StringTokenizer ("2@f.c", "@");
            int n = fenxi.countTokens ();
             if (n> = 3)
                 {
                 System.out.println ("OK");
                 }
             else
                {
                
                  System.out.println ("Error");
                   
                }
}
}

Error
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-2 08:00:02
| Show all posts
Determine whether the mailbox meets the rules.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-2 16:15:01
| Show all posts
This split should be determined by the "person_email" string, depending on how the landlord started to spell this string. You should first give the "person_email" string to see. You know
Reply

Use magic Report

1

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-3 13:45:02
| Show all posts
After testing it, I think it should be n> = 2 to get the correct result.

But 1 @. Is also OK

To put it bluntly, this code is to see if the email you entered is separated into two parts by "@"

I think it can be solved with the Split method
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list