String类和异常
Stirng类
1、创建
1)常量方式创建
String str=“I love Mr.Ceng!”;
2)构造方法来创建
String s=new String(str);
char chars[]={‘a’,‘b’,‘c’,‘d’}
String s1=new String(chars);
3)静态方法valueOf方法来创建(重载)
Stirng s2=String.valueOf(true);//将boolean类型的true转换成字符串对象的‘true’;
4)通过连接操作符(+)来创建
String s3=“abc”+123;//s3的值是abc123
String类的常用方法
char charAt(int index)//返回字符串对象中指定索引处的字符;
char[] toCharArrary()//将字符串对象转换为字符数组;
String concat(String s)//将字符串s连接到当前字符串对象的末尾,返回转换后的新串;
Boolean equals(Object obj)//判断字符串值是否与obj对象的值相同;
int indexOf(String str)//从当前字符串对象的0索引处检索参数str,返回字符串str首次出现的位置;
1、判断Email地址是否合法;
package test6yichang;
import name.Table;
public class Demo {
public static void main(String[] args) {
String s="abc@126.com";
System.out.println(EmaiValidate(s)?"合法":"不合法");
}//主函数
public static boolean EmaiValidate(String email)
{
boolean tag=true;
int length=email.length();
if(email.indexOf('@')<0)//判断@字符是否存在
tag=false;
else
{
int countAt=0;
for(int i=0;i<length;i++)
{
if(email.charAt(i)=='@')
{
countAt++;
}
}
if(countAt>1)
{
tag=false;
} else
{
if(email.indexOf('@')==0||email.indexOf('@')==length-1
||email.indexOf('.')<0||email.indexOf('.')==0||email.indexOf('.')==length-1)
tag=false;
}
}
return tag;
}
}
2、某学校请假格式;某某同学,请假几天,因为什么事情请假;
package test6yichang;
public class demo1 {
public static void main(String[] args) {
String s="小张同学,10日,因为家里有事需要请假";
System.out.println(s.substring(0,2));
System.out.println(s.substring(4,8));
System.out.println(s.substring(s.indexOf(','),9));
}
}
3、记录一个子串在整串中出现的次数
package test6yichang;
//记录一个子串在整串出现的次数
public class demo2 {
public static void main(String[] args) {
String s="nbaernbatnnaynbauinbaopnba";
String k="nba";
System.out.println(getKeyStringCount(s,k));
}
public static int getKeyStringCount(String str,String key)
{
int count=0;
if(str!=null)
{
for (int i = 0; i < str.length() - 2; i++) {
String temp = str.substring(i, i + key.length());
if (temp.compareTo(key) == 0) {// 创建临时String temp与match匹配
count++;
}
}
}
return count;
}
}
4、已知正确的用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登陆后,给出相应提示。
package test6yichang;
import java.util.Scanner;
public class demo3 {
public static void main(String[] args) {
String mame = "tianguancifu";
String password = "baiwujingji";
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
if (name.equals(name) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if (2 - i == 0) {
System.out.println("您的账户已经被锁定,请您与管理员取得联系");
break;
} else {
System.out.println("您的输入有误,请重新输入,你还有" + (2 - i) + "次机会");
}
}
}
}
}
5、编写一个学生类:
属性为:姓名,年龄
创建学生对象,并打印学生信息(都来自于键盘录入)
1)设计俩个异常类:当输入姓名长度320个字符时为正常,其他长度抛出异常,当年龄超过1120时,抛出异常。
2)编写测试类:在main程序将捕捉这个异常,并作出相应处理(输出异常)
package test6yichang.demo4;
public class Student {
String name;
int age;
public Student() {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
//异常
public class StudentAgeException extends RuntimeException{
public StudentAgeException(){
}
public StudentAgeException(String message){
super(message);
}
}
public class StudentNameException extends RuntimeException{
public StudentNameException(){
}
public StudentNameException(String message){
super(message);
}
}
//测试代码
package test6yichang.demo4;
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Student stu=new Student();
Scanner sc=new Scanner(System.in);
int age;
while(true){
try {
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
stu.setName(name);
System.out.println("请输入学生年龄:");
age = Integer.parseInt(sc.nextLine());
stu.setAge(age);
break;
} catch (NumberFormatException e){
System.out.println("年龄输入有误,请输入整形年龄!");
}catch (StudentNameException e){
System.out.println(e);
}catch (StudentAgeException e){
System.out.println(e);
}
}
System.out.println(stu);
}
}