Java exercises (String Class): Display 4-bit verification code, output date, fetch information from XML

Source: Internet
Author: User

1. Display 4-digit verification Code

Note: Uppercase and lowercase letters, mixed numbers

 Public Static void Main (string[] args) {                String s= "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";   Sets the set of verification codes        String s1= ""                ;  for (int i=0;i<4;i++) {            int n= (int) (Math.random () *1000)%s.length ( ); // take the remainder as an index to find the verification code            S1+=s.substring (n,n+1);//index starts with N and ends at N+1                      }                System.out.println (S1);            }

2. Output Date

         Public Static voidMain (string[] args) {//method One://First step: Make a Calendar objectCalendar c=calendar.getinstance (); //The second step of making a formatSimpleDateFormat f=NewSimpleDateFormat ("Yyyy-mm-dd hh-mm-ss");//h for 24-hour, H for 12-hour system//Step three: FormattingString s=F.format (C.gettime ());                        System.out.println (s); //Method Two://First step: Make a Calendar objectCalendar c2=calendar.getinstance (); //Step Two: Extract date elements        intYear=C2.get (calendar.year); intMonth=C2.get (Calendar.month); intday=C2.get (Calendar.day_of_month); //Step three: Stitching displayString s2=year+ "Year" +month+ "month" +day+ "Day";                    SYSTEM.OUT.PRINTLN (S2); }                        

3. Fetching information from XML

 public  static  void   main (string[] args) {
String s =" <student>< Name> Xiao Ming </name><sex> male </sex></student> "; s =s.replaceall ("<student>", ""). ReplaceAll ("</student>", " <student></student> Replace with null characters
int n=s.indexof ("</name>"
String name=s.substring (6,n); // the first 6 characters of "Xiaoming"
int s1=s.indexof ("<sex>") +5; // " male" before "<sex>" index Plus "<sex>" itself length 5 characters
int s2=s.indexof ("</sex>"
String Sex=

SYSTEM.OUT.PRINTLN (name+
}

Java exercises (String Class): Display 4-bit verification code, output date, fetch information from XML

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.