2015-04-30 59 views
2

我应该编写一个读取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; 
} 

}

+0

显然,你想与您的代码帮助。 1)你想让别人给你解决方案吗?或者2)你是否希望有帮助的指针帮助你自己解决它?如果你想要2,你应该在你的帖子中声明,也许是以大胆的方式阻止人们给你答案。 – Scott

+0

@scott谢谢。我想要的是指针而不是答案。我在一个编辑中粗体显示了它,以及对我所想到的代码的一些更改。 –

+0

首先我会建议看看validSSN(String s)。你传递一个字符串,说'123-45-6789'。你想要一种方式来循环通过这个字符串拉出每个字符。在循环每个字符时,请检查isDigit(char c)和isDash(char c)中的每一个字符。你应该能够谷歌如何“循环通过java中的每个字符串中的字符”来找到你的第一个答案。 – Scott

回答

1

我会画出一些我认为你可以轻松解决的一些逻辑和谷歌搜索。

  1. 正如我评论,你需要通过你传递到validSSN(String s)字符串中的每个字符来找到一种方式来循环,

例如:

s = '123-45-6789' 

你想循环遍历1,然后2等。这些字符中的每一个都保持字符串's'中的位置,从索引0到10开始。

谷歌它,看看你找到。如果您没有看到有用的东西,请查看this link

  • 正如你循环通过每个烧焦要测试每一个以知道,如果它是一个数,您的isDigit(char c)方法,或破折号,用于isDash(char c)
  • 谷歌“检查字符是否是数字java”。如果您没有找到任何东西,请参阅link。测试如果char是一个破折号,' - '应该很容易找到。

    这应该照顾isDigit(char c)isDash(char c)

  • 您需要实现在validSSN(String s)一些逻辑,使得当你循环通过每个字符,则检查:
  • a)如在适当的索引的字符是位,否则返回false

    二)如果在适当的索引字符是一个破折号,否则返回false

    如果所有字符传递你的逻辑,然后返回true。

    你也有你的主要一些代码,我不能确定到底是怎么回事,即

    String case1 = input.nextLine(); 
    String case2 = input.nextLine(); 
    String case3 = input.nextLine(); 
    String case4 = input.nextLine(); 
    

    但我认为一旦你通过1. 3.工作,你将所有设置。

    [编辑解决您的错误:

    尾翼警报如果你保持阅读我放弃了答案。所以,坚持下去,如果你放弃,请看下面的内容,弄清楚为什么这会起作用,而你的代码却没有。

    因此,对于if-else逻辑,我倾向于首先测试是否有错误,可能会出现几种情况,因此当您完成所有错误时,请返回true。可能有更好的方法来将以下代码,但我认为这是可以理解的:

    public static boolean validSSN(String s){ 
        // don't bother doing anything else if the length is wrong 
        if (s.length() != 11) { return false; } 
        else { 
         for(int i = 0; i < s.length(); i++) { 
          char c = s.charAt(i); 
          // now that you have c, use it. Don't do s.charAt(i) again. 
    
          // if index is both {not 3 AND not 6} do... 
          if ((i != 3) && (i != 6)) { 
    
           // you don't need to check "isDigit(s.charAt(i))==false" 
           if (!isDigit(c)) { return false; } // if not numeric, return false 
          } 
          // here we are either index i=3 OR i=6, so if c is not a dash return false 
          else if (!isDash(c)) { return false; } 
         } 
         // at this point we exhausted our loop and couldn't 
         // find anything false, so return true 
         return true;   
        } 
    }  
    

    编辑完]

    +0

    您复制的最后四行代码是我正在使用的实际数字。老师希望我们在桌面上创建一个文件,并从该文件中读取SSN。 情况1 = 123-56-7890 情况2 = 123-56-789 壳体3 = 123-567-890 情况4 = 123/56/7890 我提出的编辑我的代码。它看起来很长,我觉得我快到了。现在我的问题在于我的for循环,当它跨过s时。charAt(3),它返回错误,因为它不符合isDigit,它不应该这样做。但现在它使整个数字返回false。 –

    +0

    我懂了!我确定有一种更简单的方法,但如果它工作,如果有效。再次感谢。 –

    0

    为了验证破折号:

    StringBuilder caseStringBuilder = new StringBuilder(String.valueOf("346-45-3456")); 
    if(caseStringBuilder.substring(3,4).equalsIgnoreCase("-") && caseStringBuilder.substring(6,7).equalsIgnoreCase("-")){ 
        System.out.println("Dashes validated successfully"); 
    }else{ 
        System.out.println("Dash validatioin failed"); 
    } 
    

    刚刚获取的子串在破折号和比较的位置。

    为了检查数字字段:

    StringBuilder caseStringBuilder = new StringBuilder(String.valueOf("346-45-3456")); 
    if((caseStringBuilder.substring(0,3)+caseStringBuilder.substring(4,6)+caseStringBuilder.substring(7,11)).matches(".*\\d.*")){ 
          System.out.println("It contains only numbers"); 
         } 
    

    再次获取的子串并添加他们。然后运行验证那些是数字或不。

    让我知道你是否需要任何进一步的细节。

    +0

    我们还没有使用StringBuilder。尽管感谢您的输入! –