Java (Experiment 4) Java Standard Class Library-Turn lowercase letters in a string into uppercase letters, and turn uppercase letters into lowercase letters

1. The purpose of the experiment:
1. Learn how to use string String and Stringtime 2. How to use
simple regular expression 3. How to use
date and calendar
4. Use data type wrapper for data type conversion
2. Experiment environment :
JAVA + Window + Eclipse
III. Experimental content:
1. Turn lowercase letters in a string into uppercase letters, and turn uppercase letters into lowercase letters

package code4;
import java.util.Scanner;
public class str {
    
    
     public static String StringChange(String s){
    
    
     	StringBuilder a=new StringBuilder();	
     	int len=s.length();	
     	char c;	
     	for(int i=0;i<len;i++)	{
    
        
     	   c=s.charAt(i);		
     	   if(c>='a' && c<='z')		
     	   {
    
    c=(char)(c-32);}		
     	      else if(c>='A' && c<='Z')		
     	           {
    
    c=(char)(c+32);}		
     	  a.append(c);	
     	  }	
     	return a.toString();
     		} 	
public static void main(String[] args) {
    
    	
	// TODO Auto-generated method stub		
	System.out.println("请要转换的英文字符串:");		
	Scanner input=new Scanner(System.in);		
	String s=input.next();		
	System.out.println("你输入内容是:"+s);		
	System.out.println("转换后字符串为");		
	System.out.println(StringChange(s));	
	}}

2. Find out all the common character codes in the two strings:

package code4;
import java.util.Scanner;
public class array {
    
     
	public static void main(String[] args) {
    
    	
		// TODO Auto-generated method stub		
		System.out.println("输入第一个字符串:");		
		Scanner input=new Scanner(System.in);		
		String s1=input.next();		
		System.out.println("输入第二个字符串:");		
		Scanner input1=new Scanner(System.in);		
		String s2=input1.next(); 		
		for (int i=0;i<s1.length();i++){
    
          
		      for (int j=0;j<s2.length();j++){
    
               	               
		       if (s2.charAt(j) == s1.charAt(i)){
    
                  
		       System.out.println(s2.charAt(j));              
		         }    
		           }   
		             }
		             	}}

3. Define a regular expression to identify the email address of the string.

package code4;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class regex {
    
     
	public static void main(String[] args) {
    
    	
		// TODO Auto-generated method stub		
		String fileContent="张三的邮箱是:[email protected] 1212  李四的邮箱:[email protected] 王五的邮箱:[email protected]";		
		//用Pattern编译一个表达式		
		Pattern p=Pattern.compile("[a-zA-Z0-9_]{1,20}@[a-zA-Z0-9]{2,10}[.](com|cn|org)");		//通过Pattern对象得到一个Matcher对象		
		Matcher m=p.matcher(fileContent);		
		//搜索符合正则表达式的子串 调用Matcher的find方法 如果找到了匹配的子串,返回真		
		while(m.find()){
    
    		
		//取出匹配的子串用group()方法			
		System.out.print("抓取的邮箱使用group方法:"+m.group()+"  ");
		System.out.println("抓取的邮箱使用start end方法:"+fileContent.substring(m.start(), m.end()));	
			}
				}}

4. Write a program to use Map to store and query student transcripts, sort the results and store them in the TreeSet, and find the minimum and maximum average scores!

 package code4;
 import java.util.Collections;
 import java.util.Map;
 //import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.Iterator;
 import java.util.HashMap;
 public class grade {
    
     
 	public static void main(String[] args) {
    
    	
 		// TODO Auto-generated method stub		
 		int sum=0,each;		
 		HashMap<String,Integer> hm=new HashMap<String,Integer>();		
 		hm.put("A", 58);		
 		hm.put("B", 32);		
 		hm.put("C", 34);		
 		hm.put("D", 98);		
 		hm.put("E", 78);		
 		hm.put("F", 86);		
 		hm.put("G", 48);		
 		Set set2=hm.keySet();		
 		System.out.println("排序后:");		
 		TreeSet<Integer> ts=new TreeSet<Integer>();		
 		Set<Map.Entry<String,Integer>> set=hm.entrySet();
 		Iterator<Map.Entry<String,Integer>> it=set.iterator();		
 		while(it.hasNext())	
 			{
    
    			
 			each=it.next().getValue();			
 			ts.add(each);			
 			sum += each;	
 				}		
 Iterator<Integer> i=ts.iterator();		
 while(i.hasNext()) {
    
    		
 	System.out.print(i.next()+" ");		
 	}		
System.out.println();		
System.out.println("最大值为:"+Collections.max(ts));		
System.out.println("最小值为:"+Collections.min(ts));		
System.out.println("平均值为:"+sum/ts.size());	
}}

5. Given an integer -1234567, output its binary, octal, and hexadecimal representations

package code4;
import java.util.Scanner;
public class zheng {
    
     	
public static void main(String[] args) {
    
    	
	// TODO Auto-generated method stub		 
	int n;		
	 System.out.println("输入一个整数:");		 
	 Scanner s=new Scanner(System.in);		 
	 n=s.nextInt();		
         System.out.println("二进制:" + Integer.toBinaryString(n));     		 
         System.out.println("八进制:" + Integer.toOctalString(n));       		 
         System.out.println("十六进制:" + Integer.toHexString(n));	
         }}

6. Write a program to check the Java version and class path code of the current system:

package code4;
public class version {
    
     
	public static void main(String[] args) {
    
    	
		// TODO Auto-generated method stub		
		System.out.println("版本:");		System.out.println(System.getProperty("java.version"));		
		System.out.println("类路径:");		System.out.println(System.getProperty("java.class.path"));	
		}}

7. Write JAVA program, call the external program cmd in it, and display the output result.

package code4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class cmd {
    
     
	public static void main(String[] args) {
    
    	
		// TODO Auto-generated method stub					
		BufferedReader br = null;					
		try {
    
    						
		Process p = Runtime.getRuntime().exec("net user");
	        br = new BufferedReader(new InputStreamReader(p.getInputStream()));						
		String line = null;						
		while ((line = br.readLine()) != null) {
    
    
			System.out.println(line);				
		     }					
		}catch (Exception e) {
    
    					
			e.printStackTrace();				
			    } finally {
    
    						
			     if (br != null) {
    
    			
			     try {
    
    							
				br.close();						
				}catch (Exception e) {
    
    
				e.printStackTrace();						
					}					
						}	     
						  }	
	}	}

8. Please use the java.Text.SimpleDateFormat class to format the date, in the form of August 10, 2005.

 package code4;
 import java.text.SimpleDateFormat; 
 import java.util.Date;
 public class time {
    
     
 	public static void main(String[] args) {
    
    	
 		// TODO Auto-generated method stub		
 		Date ss = new Date(); 		
 		System.out.println("一般日期输出:" + ss); 		
 		SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 		 		String time = format0.format(ss.getTime());		
 		SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
 		 		time = format1.format(ss.getTime());		
 		System.out.println("格式化结果1:" + time); 	
 			}	 
 	}

Fourth, experience:
This experiment learned the use of string String, Stringtime class date and calendar class and the use of simple regular expressions. There is also the conversion of character arrays to strings. The String class contains many frequently used methods that can be called directly. I learned a lot of knowledge, such as discovering that every time string defines a new object, it will open up space in the new stack to store the value. For String iteration, you can use a for loop, use charAt(i) to get, or use for(Character c: A) to traverse, etc. However, there are still many contents that are not well understood and will take time to study in depth.

Guess you like

Origin blog.csdn.net/haha_7/article/details/109151992