【发布时间】:2015-04-30 03:17:03
【问题描述】:
我应该编写一个程序,将 SSN 作为字符串读取,包括破折号,例如 DDD-DD-DDDD,其中“D”是一个数字。如果它有效,它会打印“valid 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 读取字符串,但除此之外,我不知所措。这是我目前所拥有的:
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;
}
}
我更喜欢帮助而不是答案,因为我刚刚学习如何编码。
回答
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 谢谢。我想要指针而不是答案。我在编辑中将其加粗,并对我想出的代码进行了一些更改。
-
首先我建议查看validSSN(String s)。您正在传递一个字符串,例如“123-45-6789”。您想要一种方法来循环遍历此字符串并拉出每个字符。当您遍历每个字符时,检查 isDigit(char c) 和 isDash(char c) 中的每个字符。你应该能够谷歌如何“遍历java中字符串中的每个字符”来找到你的第一个答案。
-
您的代码几乎可以工作。你也有一个严重的错误。在这个字符串“123-45--789”上测试你的代码,你会看到它也通过了。原因是您只检查第一个字符,如果它通过了您的逻辑,则返回 true。你永远不会检查下一个字符。所以重新考虑你的循环,这样你就可以将 return true 拉出 for 循环。
-
我还建议在您认为代码可行时调试您的代码。并尝试一些您知道它应该失败的简单案例。学习如何调试是非常宝贵的。
标签: java string validation io boolean-expression