Implementing Luhn's algorithm to validate a credit card number in Java I must implement Luhn's algorithm in a program to determine whether a given credit card number is valid or not, in addition I must print the name of the company that offers that credit card number. I believe this requires taking in the input as a string and converting each digit and testing if the number of digits in the input is in the valid range, which is 13 to 16. 

I have paste my code below, would it be possible to have it reviewed and to receive feedback on whether or not my methods are correct and on issues such as how I may resolve my java.lang.NullPointerException. This would all help me continue in the right direction.

CreditCard.java

import java.util.Scanner;

public class CreditCard {

    private int evenSum;
    private int oddSum;
    private int sum;
    private String ccNumber;
    private String company;

    public boolean isDivisibleBy10() {
        return (getEvenSum() + getOddSum()) %10 == 0;
    }

    public boolean isValid() {
        return (validateNumber() &amp;&amp; validateCompany());
    }

    public boolean validateCompany() {

        char[] ccArray = ccNumber.toCharArray();

        if(Character.getNumericValue(ccArray[0]) == 4) {
          company = "Visa";
          return true;
        }

        if(Character.getNumericValue(ccArray[0]) == 5) {
          company = "MasterCard";
          return true;
        }

        if(Character.getNumericValue(ccArray[0]) == 37) {
          company = "American Express";
          return true;
        }

        if(Character.getNumericValue(ccArray[0]) == 6) {
          company = "Discovery";
          return true;
        }
        return false;
      }


    public boolean validateLength() {
        String cardNumber = "";
        for (int  i = 0; i &gt;= 16; i++){
          cardNumber += "1";
          while(i &lt;= 16) {
            if (i &gt;= 13) {
              System.out.println("valid length");
            } else {
              System.out.println("invalid length");
              break;
            }
          }
        }
        return false;
    }


    public boolean validateNumber() {
        // Luhn's algorithm
        long cardNumber = 0;
        long n = cardNumber;
        int sum = 0;
        int count = 0;

        while (n &gt; 0)
        {
            int digit = (int)(n % 10);
            count++;
            if (count % 2 == 1){
                sum = sum + digit;
            }
            else if (digit&lt;5){
                sum = sum + 2 * digit;
            }
            else{
                sum = sum + (2 * digit) - 9;
            }
            n = n/10;
        }
        return (sum % 10 == 0);
    }

    public boolean validateSums() {
        return isDivisibleBy10();   
    }

    public CreditCard(String num) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter credit card number: ");
        num = input.nextLine();
        num = "0;";
    }

    public int getEvenSum() {
        return evenSum;
    }

    public int getOddSum() {
        return oddSum;
    }

    public int getSum() {
        sum = evenSum + oddSum;
        return sum;
    }

    public String getCcNumber() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number for a credit card number: ");
        ccNumber = input.nextLine();
        return ccNumber;    
    }

    public String getCompany() {
        return company;
    }
}


tester.java

import java.util.Scanner;

        public class tester {

            public static void main(String[] args) {

                CreditCard cC = new CreditCard(null);

                System.out.print(cC.isValid()); 

            }
        }
Ordering

The ordering is a bit mixed. Try to an ordering such as: variables, constructor, public methods, private methods. There's no real convention for that, but try to tell the reader the story about your class. As for instance the member vars are the protagonists, the Constructor is the entry of a book, and the methods are the chapters.

Probable bugs


Even and odd sum are never assigned


simplify


In validateCompany, you can assign the char once and use a switch-statement.
In validateLength: String has a length() method.
In isDivisibleBy10: You call getEvenSum and getOddSum, add both values up, even though you have a getSum method.


concerns


The validateCompany method assigns the company. On one hand, the developer who uses your api would never figure that out. Also, you have to call validate, otherwise you wouldn't get the company
The constructor gets a credit card number passed. At the same time it reads from the console. And then ignores the input and assignes "0;" to the local variable. Only pass the number, that should be enough, the type shouldn't care about how the number is loaded.
getCcNumber: This method too should not read from the console. It should return the value which was passed to the constructor.


public methods

The only method you call in your tester class (which should start with a capitalized T), only calls the isValid() method. Reduce the visibility of all other methods. Depending on how the type is used, the user of the API will see too many methods, he only needs one. And beside that: It's a common principle to hide as much implementation as you can, also known as 'information hiding'

Hope this helps,
slowy