Home > Java > javaTutorial > Share a basic example of JAVA programming

Share a basic example of JAVA programming

零下一度
Release: 2017-07-20 10:37:07
Original
1608 people have browsed it

Familiar with the use of the String method to determine whether the file name is legal

After studying, write a small exercise to consolidate the application of the String method.

Task:

1. Determine the type of content the user chooses to input

2. If it is a java file, output "You entered a java file name"

3. If it is an email address, output "You entered an email address"

4. If neither, output "Unable to parse the content you entered"

The code is as follows:

 1     public static void main(String[] args) { 2         
 Scanner in = new Scanner(System.in); 3         
 boolean flag = true; 4         do { 5             
 System.out.println("请您要判断的内容:"); 6             
 String name = in.next(); 7  8             switch (Jude(name)) { 9             
 case 1:10                 System.out.println("您输入的是java文件名");11                 
 flag = false;12                 break;13             case 2:14                 
 System.out.println("您输入的是邮箱地址");15                 
 flag = false;16                 break;17             case -1:18                 
 System.out.println("无法解析您输入的内容,请重新输入!");19                 
 break;20             }21         } while (flag);22         
 in.close();23     }24 25     /*26      * 判断格式27      */28     
 private static int Jude(String Name) {29 30         
 int count1 = 0;// 字符串中包含.的个数31         
 int count2 = 0;// 字符串中包含@的个数32 33         
 for (int i = 0; i < Name.length(); i++) {// 统计个数34             
 if (Name.charAt(i) == &#39;.&#39;) {35                 count1++;36             }37             
 if (Name.charAt(i) == &#39;@&#39;) {38                 count2++;39             }40         }41         
 if (count1 == 1 && count2 == 1) {42             // 获取邮箱中"@"符号的位置43             
 int index2 = Name.indexOf(&#39;@&#39;);44             // 获取邮箱中"."号的位置45             
 int index3 = Name.indexOf(&#39;.&#39;);46             
 if (index2>0 && index3-index2> 1&&index3!=Name.length()-1) {// 判断必须包含"@"符号,且"@"必须在"."之前47  
                48                 return 2;49             }50         }51         if (count1 > 0) {52             // 获取文件名中最后一次出现"."号的位置53             int index = Name.lastIndexOf('.');54             // 获取文件的后缀55             String prefix = Name.substring(index);56             57             // 判断必须包含"."号,且不能出现在首位,同时后缀名为"java"58             if (index != -1 && index != 0 && prefix.equals(".java")) {59 60                 return 1;61             }62         }63         return -1;64 65     }
Copy after login

Run result:

The above is the detailed content of Share a basic example of JAVA programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template