1.给定一个test.txt的日志文件,文件的内容如下:
20201011 103232 Exception: Null Pointer
20201011 101433 Exception: Null Pointer
20201012 093232 Exception: Null Pointer
20201013 141433 Exception: Type error
20201013 103232 Exception: testerror
20201013 101433 sdfsdfsdf
20201014 103232 wefwef
20201015 101433 Exception: Null Pointer
要求以小时为节点,打印出每个小时内错误类型出现的次数,格式如下:
20201011 10 Exception: Null Pointer 2
20201013 10 Exception: PPOO Pointer 1
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DemoTest9 {
public static void read(String file) throws IOException {
BufferedReader bw = new BufferedReader(new FileReader(file));
String line;
Map<String,Integer> map = new TreeMap<>();
while ((line = bw.readLine())!= null) {
if (line.contains("Exception:")) {
String pattern = "([0-9]{8} [0-9]{2})";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
String key =m.group()+" Exception:"+line.split("Exception:")[1]+" ";
if(map.containsKey(key)){
int value = map.get(key)+1;
map.put(key,value);
}else {
map.put(key,1);
}
}
}
}
for(String key:map.keySet()){
System.out.println(key+map.get(key));
}
}
public static void main(String[] args) throws IOException {
DemoTest9.read("src/main/java/testarray/a.txt");
}
}
2.[ i+3 for i in range(10)],写出输出结果:
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
两个大数相加
思路:长度不一致,短的那个数字往前补0
package testarray;
//两个大数之和
public class DemoTest10 {
public static void main(String[] args) {
String s1="12121267";
String s2="123246788";
System.out.println(DemoTest10.sum(s1,s2));
System.out.println(12121267+123246788);
}
public static String sum(String str1,String str2){
StringBuilder s1 =new StringBuilder();
StringBuilder s2= new StringBuilder();
StringBuilder result = new StringBuilder();
int t;
String tmp ="";
int max = str1.length()>str2.length()?str1.length():str2.length();
if(str1.length()>= str2.length()){
t = str1.length() - str2.length();
for(int i=0;i<t;i++){
tmp+=0;
}
s2.append(tmp).append(str2);
s1.append(str1);
}else {
t = str2.length() - str1.length();
for(int i=0;i<t;i++){
tmp+=0;
}
s1.append(tmp).append(str1);
s2.append(str2);
}
System.out.println("s1:"+s1.toString());
System.out.println("s2:"+s2.toString());
System.out.println("max:"+max);
int num=0;
for(int j=max-1;j>=0;j--){
int m =s1.charAt(j)-'0';
int n = s2.charAt(j)-'0';
int sum =m+n;
if (num !=0) {
sum+=1;
num=0;
}
if(sum < 10){
result.append(sum);
}else {
result.append(sum%10);
num = sum/10;
}
System.out.println(result);
}
return result.reverse().toString();
}
}