1

I'm working on a Credit Card Validator program, but I would also like to implement code that identifies the type of credit card entered by its first numerical value. How do I identify string by its first numerical value?


import java.util.Scanner;

public class parking_garage {

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



                // Prompt the user to enter a credit card cardnumber as a long integer
                System.out.print("Enter a credit card cardnumber as a long integer: ");
                long cardnumber = input.nextLong();

                System.out.println(
                    cardnumber + " is " + (isValid(cardnumber) ? "valid" : "invalid"));
    }



            /** Return true if the card cardnumber is valid */
            public static boolean isValid(long cardnumber) {
                boolean valid =
                    (getSize(cardnumber) >= 13 && getSize(cardnumber) <= 16) &&
                    (prefixMatched(cardnumber, 4) || prefixMatched(cardnumber, 5) ||
                    prefixMatched(cardnumber, 37) || prefixMatched(cardnumber, 6)) &&
                    ((sumOfDoubleEvenPlace(cardnumber) + sumOfOddPlace(cardnumber)) % 10 == 0);

                return valid;
            }

            /** Get the result from Step 2 */
            public static int sumOfDoubleEvenPlace(long cardnumber) {
                int sum = 0;
                String num = cardnumber + "";
                for (int i = getSize(cardnumber) - 2; i >= 0; i -= 2) {
                    sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);
                }
                return sum;
            }

            /** Return this cardnumber if it is a single digit, otherwise,
            * return the sum of the two digits */
            public static int getDigit(int cardnumber) {
                if (cardnumber < 9)
                    return cardnumber;
                else
                    return cardnumber / 10 + cardnumber % 10;
            }

            /** Return sum of odd-place digits in cardnumber */
            public static int sumOfOddPlace(long cardnumber) {
                int sum = 0;
                String num = cardnumber + "";
                for (int i = getSize(cardnumber) - 1; i >= 0; i -= 2) {
                    sum += Integer.parseInt(num.charAt(i) + "");
                }
                return sum;
            }

            /** Return true if the digit d is a prefix for cardnumber */
            public static boolean prefixMatched(long cardnumber, int d) {
                return getPrefix(cardnumber, getSize(d)) == d;
            }

            /** Return the cardnumber of digits in d */
            public static int getSize(long d) {
                String num = d + "";
                return num.length(); }

            /** Return the first k cardnumber of digits from cardnumber. If the
            * cardnumber of digits in cardnumber is less than k, return cardnumber. */
            public static long getPrefix(long cardnumber, int k) {
                if (getSize(cardnumber) > k)  {
                    String num = cardnumber + "";
                    return  Long.parseLong(num.substring(0, k));
                }
                return cardnumber;
            }
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • I'm not sure if it can happen but by using `long` to store the number, you may have problems with CCs that start with 0, since it will just be removed from the number. – luizfzs Jun 29 '17 at 19:28
  • "_Enter a credit card cardnumber as a long integer:_" – I'd say that just a tiny minority of parking garage and credit card users knows what a _long integer_ is. And those who know will ask: Of which processor architecture and/or language? ;) – Gerold Broser Jun 29 '17 at 19:36
  • "How do I identify string by its first numerical value?" Are you asking how to get the first character of the string? – bradimus Jun 29 '17 at 19:39

2 Answers2

0

you could do something like this

public boolean isNumeric(String str)
{
    return str.matches("-?\\d+(\\.\\d+)?");  
}

public void randomName(String str){
   for (int i =0 ;i<str.Length;i++){
      if(isNumeric(str.charAt(i)+"")){
         //Do THING
         break;
      }
   }
}

This will loop through all values in the string and once a number is found //Do THING will run, which is where you should put your code

Robert Juneau
  • 652
  • 6
  • 15
0

Credit card Starts with certain number Like

VISA -4
MASTERCARD -5
AMEX - 3
DISCOVER -6

After I post my Answer I found this on github. https://gist.github.com/gabrielbauman/f3db7ce8ae7828fa05b3

want2learn
  • 2,471
  • 2
  • 20
  • 37