-2

I've been working on a program that is supposed to analyze a credit card number that the user enters in, including info regarding its company, whether it is valid or not, and so on.

My issue is that I continue to run the NoSuchElementException, and from what I have read it seems that one of my loops continues to run, but I'm not sure where, or why.

import java.util.*;
public class CreditCard {

  public static void main (String[] args) {
    Scanner in = new Scanner (System.in);
    long nums = 0;
    int length;
    System.out.print("Enter 15 or 16-digit credit card number: ");
    long numsEntered = in.nextLong();
    if(isValid(nums) == true) {
      System.out.println(nums + " is valid.");
    } else {
      System.out.println(nums + " is invalid.");
    }
  }

  public static boolean isValid (long cc_num) {
    long numsEntered;
    int total = sumOfOdd(cc_num) + sumOfEven(cc_num);
    return (total % 10 == 0) && (prefixMatched(cc_num, 1) == true) && 
        (getSize(cc_num)>=13) && (getSize(cc_num)<=16);

  }

  public static int sumOfEven(long number) {
    int doubleEven = 0;
    long place = 0;
    while (number > 0) {
      place = number % 100;
      doubleEven += getDigit((int) (place / 10) * 2); 
      number = number / 100;
    }
    return doubleEven;
  }

  public static int sumOfOdd(long number) {
    int odd = 0;
    while (number <=9) {
      odd += (int)(number % 10);
      number = number % 100;
    }
    return odd;
  }

  public static int getDigit(int number) {
    if (number <= 9) {
      return number;
    } else {
      int firstNum  = number % 10;
      int secondNum = (int)(number / 10);
      return firstNum + secondNum;
    }
  }

  public static boolean prefixMatched(Long number, int d) { 
    if((getPrefix(number, d) == 3) || (getPrefix(number, d) == 4) || (getPrefix(number, d) == 5)) {
      if(getPrefix(number, d) == 3) {
        System.out.println("Visa");
      } else if (getPrefix(number, d) == 3) {
        System.out.println("Amex");
      } else if (getPrefix(number, d) == 5) {
        System.out.println("Master Card");
      }
      return true;
    } else {
      return false;
    }
  }

  public static int getSize(long d) {
    int count = 0;
    while (d >0) {
      d = d/10;
      count++;
    }
    return count;
  }

  public static long getPrefix(long number, int k) {
    if(getSize(number) < k) {
      return number;
    } else {
      int size = (int)getSize(number);
      for (int i = 0; i < (size - k); i++) {
        number = number / 10;
      }
      return number;
    }
  }
}

The exact error i continue to get is:

Enter 15 or 16-digit credit card number: Exception in thread "main" 

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextLong(Scanner.java:2222)
at java.util.Scanner.nextLong(Scanner.java:2182)
at CreditCard.main(CreditCard.java:11)
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

1 Answers1

0

Well, you are calling isValid with num. Shouldn't you call isValid with numsEntered instead?

Random guy
  • 53
  • 8