I am working on a Luhn's test problem and I would like to build the program using a 1-D array. I have initialized an array with 16 digits but I would like to be able to initialize the array depending on how many digits the user enters.
//create scanner object
Scanner input = new Scanner(System.in);
//declare variable
long [] cc_num = new long[16];
//get input
System.out.print("Enter 15 or 16-digit credit card number: ");
//long cc_num = input.nextLong();
for (int i = 0; i < cc_num.length; i++) {
cc_num[i] = input.nextLong();
}
How can I initialize an array depending on the length of the input (15 or 16 is requested)
nextLong()is confusing. If you want the user to enter a 15- or 16-digit number, the firstnextLong()will slurp up the entire number (unless they put spaces between every digit). The secondnextLong()will wait for another credit card number.longa bit of overkill if you want each array element to store one digit? Not that it's wrong, but it leads me to believe that you haven't quite decided what values your variables are supposed to hold.