我应该编写一个读取SSN为字符串的程序,包括破折号,如DDD-DD-DDDD,其中'D'是数字。如果它是有效的,它将打印“有效的ssn”,如果它无效则显示相反的结果。我应该有四个方法:main,public static boolean validSSN(String s),public boolean isDigit(char c)和public boolean isDash(char c)。最后两个应该在方法public static boolean validSSN(String s)中调用。我们正在翻阅字符串和文本I/O。我知道如何使用java.io.File读取字符串,但除此之外,我不知所措。以下是我迄今为止:使用字符串的SSN验证
import java.io.File;
import java.util.Scanner;
public class Lab14 {
public static void main(String[] args)throws Exception {
File file = new File("C:\\Documents and Settings\\Russell\\Desktop\\Social-Security-Numbers.txt");
Scanner input = new Scanner(file);
String case1 = input.nextLine();
String case2 = input.nextLine();
String case3 = input.nextLine();
String case4 = input.nextLine();
input.close();
System.out.println("The first case is: " + case1 + ". This is a " + validSSN(case1) + " entry.");
System.out.println("The second case is: " + case2 + ". This is a " + validSSN(case2) + " entry.");
System.out.println("The third case is: " + case3 + ". This is a " + validSSN(case3) + " entry.");
System.out.println("The fourth case is: " + case4 + ". This is a " + validSSN(case4) + " entry.");
}
public static boolean validSSN(String s){
if (s.length() == 11){
if((isDash(s.charAt(3))==true) && isDash(s.charAt(6))==true){
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(isDigit(s.charAt(i))==false)
return false;
}
}
return true;
}
else
return false;
}
public static boolean isDigit(char c){
if((c == '0') || (c == '1') || (c == '2') || (c == '3') ||
(c == '4') || (c == '5') || (c == '6') || (c == '7') ||
(c == '8') || (c == '9'))
return true;
else
return false;
}
public static boolean isDash(char c){
if(c == '-')
return true;
else
return false;
}
}
我喜欢帮助,而不是答案,因为我刚才学习如何编写代码。
ANSWER
import java.io.File;
import java.util.Scanner;
public class Lab14 {
public static void main(String[] args)throws Exception {
File file = new File("C:\\Documents and Settings\\Russell\\Desktop\\Social-Security-Numbers.txt");
Scanner input = new Scanner(file);
String case1 = input.nextLine();
String case2 = input.nextLine();
String case3 = input.nextLine();
String case4 = input.nextLine();
input.close();
System.out.println("The first case is: " + case1 + ". This is a " + validSSN(case1) + " entry.");
System.out.println("The second case is: " + case2 + ". This is a " + validSSN(case2) + " entry.");
System.out.println("The third case is: " + case3 + ". This is a " + validSSN(case3) + " entry.");
System.out.println("The fourth case is: " + case4 + ". This is a " + validSSN(case4) + " entry.");
}
public static boolean validSSN(String s){
if (s.length() == 11){
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(isDigit(s.charAt(i))==true && ((isDash(s.charAt(3))==true) && isDash(s.charAt(6))==true)){
return true;
}
else
return false;
}
return false;
}
else return false;
}
public static boolean isDigit(char c){
if((c == '0') || (c == '1') || (c == '2') || (c == '3') ||
(c == '4') || (c == '5') || (c == '6') || (c == '7') ||
(c == '8') || (c == '9'))
return true;
else
return false;
}
public static boolean isDash(char c){
if(c == '-')
return true;
else
return false;
}
}
显然,你想与您的代码帮助。 1)你想让别人给你解决方案吗?或者2)你是否希望有帮助的指针帮助你自己解决它?如果你想要2,你应该在你的帖子中声明,也许是以大胆的方式阻止人们给你答案。 – Scott
@scott谢谢。我想要的是指针而不是答案。我在一个编辑中粗体显示了它,以及对我所想到的代码的一些更改。 –
首先我会建议看看validSSN(String s)。你传递一个字符串,说'123-45-6789'。你想要一种方式来循环通过这个字符串拉出每个字符。在循环每个字符时,请检查isDigit(char c)和isDash(char c)中的每一个字符。你应该能够谷歌如何“循环通过java中的每个字符串中的字符”来找到你的第一个答案。 – Scott