Java test report
Class Jike second class Student ID 20188437 Name Lei
Completion Time 2019/9/22
Rating scale
Application third experiment String class
-
Purpose
-
Master the use of class String class;
-
Learn to use JDK help documentation;
-
-
Content Experiments
1. Known string: "this is a test of java" required to do the following requirements :( source code, the results screenshot).
-
The number of letters in the string s occurs statistics.
-
The string neutron count the number of times the string "is" appears.
-
The number of words in the string "is" appears in the statistics.
-
Achieve reverse the string is output.
2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user. It requires source code, the results screenshot.
3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.
-
Experiment (Please adjust your own format)
1. (a) experiment code
package Third java experiments ;
public class Thisisatestofjava {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
String s="this is a test of java";
System.out.println((","+s+",").split("s").length-1);
@ Method a: Split function string s in the "s" split position, then .length number string obtained after the split, minus 1 to obtain "s" number of occurrences
char c[]=s.toCharArray();
for(char e:c){
if(e=='s'){
count++;
}
}
@ Method 2: toCharArray function string s into an array of characters , with foreach traverse cycle of the array of characters, and determines the count
. The System OUT .println (( "," + S + "," ) .split ( "IS" .) Length -1); // supra method
System.out.println((","+s+",").split(" is ").length-1); //同上
for (int i=c.length-1;i>= 0;i--) {
System.out.print(c[i]);
}
// use a character array index decreasing output
StringBuffer buffer = new StringBuffer(s);
System.out.println("\n"+buffer.reverse());
// defined as a StringBuffer class, with StringBuffer class reverse () method of direct reverse string.
}
}
(B) operating results
2. (a) experiment code
package Third java experiments ;
import java.util.Scanner;
public class encryption and decryption {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
String S = INPUT .next (); // input a string
char C [] = S .toCharArray (); // a single character string into
for(int i=0;i<s.length();i++){
c[i]=(char)(c[i]+3);
}
//将每个字符向后移动三位
System.out.print(String.valueOf(c)); //用String.valueOf()将加密后的字符连接起来输出
}
}
(二)运行结果
3.(一)实验代码
package 第三次java实验;
public class 统计字符串 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ABC=0,abc=0,other=0;
String s="ddejidsEFALDFfnef2357 3ed";
char c[]=s.toCharArray();
for(char e:c){
if(e>='A'&&e<='Z'){
ABC++;
}
else if(e>='a'&&e<='z'){
abc++;
}
else{
if(e!=' '){
other++;
}
}
}
System.out.print("大写字母数:"+ABC+"\n小写字母数:"+abc+"\n非英文字母数:"+other);
}
}
(二)运行结果
-
总结:
本次实验主要内容为字符串的处理,要学会善于利用String类和StringBuffer类中的方法。
同一问题有多种方法可以解决,举一反三就能学到更多。
本周学习了Java常用类库中的StringBuffer类以及package包的引用等,主要是一些概念类的知识,需要课余加以记忆巩固。