Java programming exercises determine whether the Java file name is correct, determine whether the mailbox is formatted correctly, and count the number of occurrences of a word in the specified string Fu

Source: Internet
Author: User

Determine if the Java file name is correct, and determine if the mailbox format is correct

Function: Determine whether the Java file name is correct, and determine whether the mailbox format is correct. Where: The legal file name should end with. Java; legal mailbox name

in at least include "@" and require "@" before ".".

Practice Code:

public class Test {public static void main (string[] args) {//java filename String filename = "Helloworld.jav"; Mailbox String email = "[email protected]";//determine if the Java file name is correct: The valid file name should end with. Java/reference step: 1, get the last file name Occurrences of "." The position of the number 2, according to the "." To get the suffix of the file 3, Judge "." Location and file suffix *//Get the last occurrence in the file name "."                The position of the number int index = Filename.lastindexof (".");        Gets the suffix of the file string prefix = filename.substring (index); The judgment must contain the "." The suffix name is "java" if (Index>0&&prefix.equals (". Java")) {System.out.println ("Java file name is correct");} else {        SYSTEM.OUT.PRINTLN ("Invalid Java file name");} Determine if the mailbox format is correct: at least "@" should be included in the legal mailbox name, and "@" is in "." Before/* Reference step: 1, get the "@" symbol in the file name Location 2, get the mailbox "." Position 3, the judgment must contain the "@" symbol, and "@" must be in the "."                 Before *///gets the position of the "@" symbol in the mailbox int index2 = Email.indexof ("@"); Get the "." In the mailbox        The position of the number int index3 = Email.indexof ('. '); The judgment must contain the "@" symbol, and "@" must be in the "." Before if (index2! =-1 && index3 > Index2) {System.out.println ("Mailbox format is");}   else {System.out.println ("Invalid mailbox Format");} }}

Operation Result:


Two statistics the number of occurrences of the character ' a ' in the specified string

Function: Counts the number of occurrences of the character ' a ' in the specified string.

Analysis: You can iterate through each character in a string to determine if it is a character a, and if so, the number of occurrences of the statistic.

Practice Code:

public class Test {public    static void Main (string[] args) {//defines a string s = "Aljlkdsflkjsadjfklhasdkjlflkajdflwoi UDSAFHAASDASD ";                Number of occurrences int num = 0;                Loop through each character to determine if it is a character a, if yes, the number of increments for (int i=0;i<s.length (); i++) {                //get each character, determine if the character is AIF (S.charat (i) = = ' A ') {                // Cumulative statistics number num++; }}system.out.println ("The number of occurrences of character a:" + num);    }}

Operation Result:


Three outputs the number of characters in a string

Function: Write a program that outputs the number of uppercase English letters in a string, the number of lowercase English letters, and the number of other characters.

Implementation code:

public class Test {public static void main (string[] args) {//give a string s = "abaabb$&^$ #BAAb898B #@%aa";//variable initialization//capitalization The letter int cU = 0;//lowercase int CL = 0;//other characters int cO = 0;for (int i=0; i<s.length (); i++) {///first remove each character from the string char c = S.charat (i);// Determine the number of occurrences of the various characters if (C >= ' a ' && c <= ' z ') {CU + +;} else if (C >= ' a ' && c <= ' z ') {CL + +;} else {CO + +;}} Output Result: System.out.println ("Total number of strings:" + s.length ()); System.out.println ("Number of capital letters:" + CU); System.out.println ("Number of lowercase letters:" + CL); System.out.println ("Number of other characters:" + CO);}}

Operation Result:




Java programming exercises determine whether the Java file name is correct, determine whether the mailbox is formatted correctly, and count the number of occurrences of a word in the specified string Fu

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.