JAVA basics-programming exercises (3) and java Basics
Familiar with the use of the String method to determine whether the file name is valid
After learning, write a small exercise to consolidate the application of the String method.
Task:
1. determine the type of content selected for Input
2. If it is a java file, the output "you entered the java file name"
3. For an email address, output "you entered an email address"
4. If none of them exist, the output "the content you entered cannot be parsed"
The Code is as follows:
1 public static void main (String [] args) {2 records in = new records (System. in); 3 boolean flag = true; 4 do {5 System. out. println ("What You Want To determine:"); 6 String name = in. next (); 7 8 switch (Jude (name) {9 case :10 System. out. println ("You entered the java file name"); 11 flag = false; 12 break; 13 case System. out. println ("the email address you entered"); 15 flag = false; 16 break; 17 case-System. out. println ("the content you entered cannot be parsed. Please enter it again! "); 19 break; 20} 21} while (flag); 22 in. close (); 23} 24 25/* 26 * judgment format 27 */28 private static int Jude (String Name) {29 30 int count1 = 0; // The String contains. 31 int count2 = 0; // The number of @ contained in the string is 32 33 for (int I = 0; I <Name. length (); I ++) {// count 34 if (Name. charAt (I) = '. ') {35 count1 ++; 36} 37 if (Name. charAt (I) = '@') {38 count2 ++; 39} 40} 41 if (count1 = 1 & count2 = 1) {42 // get the position of the "@" symbol in the mailbox 43 int index2 = Name. indexOf ('@'); 44 // get email ". "position 45 int index3 = Name. indexOf ('. '); 46 if (index2> 0 & index3-index2> 1 & index3! = Name. length ()-1) {// The judgment must contain the "@" symbol, and "@" must be in ". "47 48 return 2; 49} 50} 51 if (count1> 0) {52 // get the last time the file name appears ". "Location 53 int index = Name. lastIndexOf ('. '); 54 // get the file suffix 55 String prefix = Name. substring (index); 56 57 // judgment must contain ". ", and cannot appear in the first place, with the suffix" java "58 if (index! =-1 & index! = 0 & prefix. equals (". java") {59 60 return 1; 61} 62} 63 return-1; 64 65}
Running result:
This is a small program written according to my own understanding. It is mainly used to familiarize myself with the use of methods of the String class. There may be many areas for improvement. If there is any error, please point it out. Thank you !!