-1

the program always outputs "Invalid". I've checked my code multiple times but cant find the error. I'm a beginner at java, your help will be appreciated.

I've looked for all possible errors and different codes of this questions but it still doesnt work

package cs50;
import java.util.*;

public class Credit {

    int arr[];
    static long credit_card;

    public static void main(String[] args) {

        Scanner sc = new Scanner (System.in);

        System.out.println("Enter credit card number : ");
        credit_card = sc.nextLong();

        Credit obj = new Credit();

        int count = (int)obj.countDigits();

        obj.digitsToArray(count);

        int sum = obj.sum(count);

        obj.check(sum,count);

    }


    //count number of digits
    long countDigits()
    {
        long n = credit_card;
        int count = 0;

        while(n>0)
        {
            n=n/10;
            count++;
        }
        return count;
    }


    //store digits in an array
    void digitsToArray(int count)
    {
        long m = credit_card;
        arr = new int[count];

        for (int i=count-1; i>=0; i--)
        {
            arr[i] = (int)m % 10;
            m = m / 10;
        }
    }


    //Calculate sum of digits 
    int sum(int count)
    {
        int sum = 0;

        for (int i=count-2; i>=0; i=i-2)
        {
            int n = arr[i]*2;
            if(n>9) 
            {
                sum = sum + n%10 + (n/10 % 10);
            }
            else
                sum = sum + n;
        }

        for (int i=count-1; i>=0; i=i-2)
        {
            int n = arr[i];
            sum = sum + n;
        }
        return sum;
    }


    //Check credit card type
    void check(int sum, int count)
    {
        if (sum%10 == 0)
        {
            if(count==15 && (arr[0]==3 && (arr[1]==4||arr[1]==7)))
            {
                System.out.println("AMEX");
            }

            else if(count==16)
            {
                if(arr[0]==5 && (arr[1]==1||arr[1]==2||arr[1]==3||arr[1]==4||arr[1]==5))
                {
                    System.out.println("MasterCard");
                }
                else if(arr[0]==4)
                {
                    System.out.println("Visa");
                }

            }

            else if(count==13 && arr[0]==4)
            {
                System.out.println("Visa");
            }
        }
        else
            System.out.println("Invalid");
    }

}

visa or mastercard number doesnt show expected outputs, always shows "Invalid".

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
Mufaddal Hakim
  • 25
  • 1
  • 2
  • 8
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – lexicore Aug 31 '19 at 07:20

1 Answers1

0

In line with lexicore's suggestion, stepping through your code, your digitsToArray() method will need to be looked at in detail.

Providing the credit card number: 5105 1051 0510 5100

the digitsToArray() method sets the arr variable to: [5, 1, 0, 5, 1, 0, 5, 1, 0, 9, -1, -4, -9, 5, 8, 0] which doesn't seem right.

Check out the link that lexicore provided, it'll take you through why and how to use it.

Khei
  • 73
  • 3
  • 11