Luhn Credit Card Validation in Java
<p>Looking for help with my java program that uses Luhn Algorithm to validate a credit card number entered by the user. The card number must be between 13 and 16 digits in length, start with either a 4 (for Visa), 5 (Master card), 37(Amex), or a 6 ( Discover). Program should take input as a long and display if the input is valid or invalid. This is what I have so far, Excuse the formatting. </p>

<p>Even when I enter a valid card number, It is printing that it is invalid and I do not know why. Also unsure how to specify the parameters of the length of the card since it must be between 13-16 characters to be valid.</p>

<pre><code>import java.util.Scanner;
public class Lunh {

  public static void main(String []args){
      Scanner input = new Scanner(System.in);
      long cardNumber;
      int sum = 0;
     boolean even = false;

      String cardType;
      System.out.println("Enter credit card number"); //prompt user to enter their card number
      cardNumber = input.nextLong();

      String cardNumberString = Long.toString(cardNumber); //card number entered as a long and converted to string 
      isValid(cardNumberString);
  }
    public static boolean isValid(String cardNumber){
    int sum = 0 ;
    boolean even = false;
    String  cardType = null;

    int firstDigit = getDigit(cardNumber,1);
    switch (firstDigit){
        case 4: 
        cardType = "Visa";
        break;
        case 3:
        if (getDigit(cardNumber,2)==7) {
            cardType= "American Express";
            } else {
            return false;
            }
        case 5:
        cardType = "Master Card";
        break;
        case 6: 
        cardType = "Discover";
        break;
    } //end card type switch

    int secondDigit = getDigit(cardNumber, 2);

    //loops through digits in reverse order right to left
    for (int i = cardNumber.length();i&gt;0; i--){
        int digit = getDigit(cardNumber, i);

        //double every other digit
        if (even)
        digit += digit;

        even = ! even;

        //if result is greater than 9 then subtract 9
        if (digit &gt; 9)
        digit = digit - 9;

        sum += digit;
        } //end of loop

    if (sum % 10 == 0) 
    {
        System.out.println(cardNumber+" is valid");
        System.out.println("Card Type: "+cardType);
        return true;
    }
    else {
        System.out.println(cardNumber+ " is invalid");
        System.out.println("Card Type: Invalid");
        return false;
    }
    } //end of isValid class

    //gets digit at specified position
    private static int getDigit(String digitString, int position){
        String characterAtPosition = digitString.substring(position - 1, position);
        return Integer.parseInt(characterAtPosition);
    }  
}
</code></pre>