package nameyu;import java.util.Scanner;public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.print("Enter a SSN:"); String ssn=input.nextLine(); if(ssn.length()==11){ if(ssn.charAt(3)=='-'&&ssn.charAt(6)=='-'){ String str1=ssn.substring(0,3); String str2=ssn.substring(4,6); String str3=ssn.substring(7); if(str1.charAt(0)>='0'&&str1.charAt(0)<='9'){ if(str1.charAt(1)>='0'&&str1.charAt(1)<='9'){ if(str1.charAt(2)>='0'&&str1.charAt(2)<='9'){ if(str2.charAt(0)>='0'&&str2.charAt(0)<='9'){ if(str2.charAt(1)>='0'&&str2.charAt(1)<='9'){ if(str3.charAt(0)>='0'&&str3.charAt(0)<='9'){ if(str3.charAt(1)>='0'&&str3.charAt(1)<='9'){ if(str3.charAt(2)>='0'&&str3.charAt(2)<='9'){ if(str3.charAt(3)>='0'&&str3.charAt(3)<='9'){ System.out.println(ssn+" is a valid social security number"); } } } } } } } } } } }else System.out.println(ssn+" is an invalid social security number"); } } 4.22检测子串 package nameyu;import java.util.Scanner;public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.print("Enter string s1:"); String str1=input.next(); System.out.print("Enter string s2:"); String str2=input.next(); if(str1.indexOf(str2)>=0){ System.out.println(str2+" is a substring of "+str1); } else System.out.println(str2+" is ont a substring of "+str1); } }
|