Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

java - How can I test credit numbers with hyphens (" -" ) to get it as INVALID . When I tried 4003-6000-0000-0014 i am getting errors

I cant get credit card numbers containing hymens as INVALID such as 4003-6000-0000-0014 must give me INVALID but its giving me errors of string.

public class prog {

public static void main(String[] args) 
{
    Scanner userInput = new Scanner(System.in) ;
    

    System.out.println("How many credit card you want to check?");
    int numOfCredit = userInput.nextInt() ;
    
    int creditnumbers[] = new int[numOfCredit] ;
    
    for (int i = 0 ; i<numOfCredit ; i++)
    {
        System.out.println("Enter credit card number "+(i+1)+": ") ;
        String creditNumber = userInput.next() ;
        
        validateCreditCardNumber(creditNumber);
        
        
    }
}


private static void validateCreditCardNumber(String str) { // function to check credit numbers

    int[] ints = new int[str.length()];
    for (int i = 0; i < str.length(); i++) {
        ints[i] = Integer.parseInt(str.substring(i, i + 1));
    }
    for (int i = ints.length - 2; i >= 0; i = i - 2) {
        int j = ints[i];
        j = j * 2;
        if (j > 9) {
            j = j % 10 + 1;
        }
        ints[i] = j;
    }
    
    
    int sum = 0;
    for (int i = 0; i < ints.length; i++) 
    {
        sum += ints[i];
    }
    if (sum % 10 == 0) 
    {
        System.out.println("VALID");
    } 
    
    else 
    {
        System.out.println("INVALID");
    }
}

}

I get these errors after running with hymens :

Exception in thread "main" java.lang.NumberFormatException: For input string: "-" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:642) at java.base/java.lang.Integer.parseInt(Integer.java:770) at testing/testing.prog.validateCreditCardNumber(prog.java:33) at testing/testing.prog.main(prog.java:22)

question from:https://stackoverflow.com/questions/65885389/how-can-i-test-credit-numbers-with-hyphens-to-get-it-as-invalid-when-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can replace in your string "-" with "" (blank) and then apply this function:

    String card = "4003-6000-0000-0014";

    Boolean t = check(card.replace("-",""));

    public static boolean check(String ccNumber)
    {
        int sum = 0;
        boolean alternate = false;
        for (int i = ccNumber.length() - 1; i >= 0; i--)
        {
            int n = Integer.parseInt(ccNumber.substring(i, i + 1));
            if (alternate)
            {
                n *= 2;
                if (n > 9)
                {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        return (sum % 10 == 0);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...