4

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)

4
  • Your use of nextLong() is confusing. If you want the user to enter a 15- or 16-digit number, the first nextLong() will slurp up the entire number (unless they put spaces between every digit). The second nextLong() will wait for another credit card number. Commented Jul 6, 2015 at 2:15
  • 1
    Take a user input for the array length, create a new array with that length, then iterate over it as normal. What's the confusion? You have all the tools at hand to do this. Commented Jul 6, 2015 at 2:15
  • 1
    Also, isn't long a 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. Commented Jul 6, 2015 at 2:17
  • How* do you get the length of the user input? @Kon Commented Jul 6, 2015 at 2:22

2 Answers 2

6

I agree to what Elliott has proposed. But I'm curious as to why you are using long data type. If you were to hold credit card into a variable, long would be the solution. 16 digit perfectly fits into long. Here is what I would use

System.out.print("Enter 15 or 16-digit credit card number: ");
String cardStr = input.nextLine();
int [] cc_num = new int[cardStr.length()];

more better way

System.out.print("Enter 15 or 16-digit credit card number: ");
String cardStr = input.nextLine();
long cc_num = Long.parseLong(cardStr);

best way

System.out.print("Enter 15 or 16-digit credit card number: ");
String cardStr = input.nextLine();
//store as string itself, so that leading 0's are preserved
//it really makes more sense because no arithemtic operations are performed on cc numbers
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think long or int should be used for "numbers" that are really just strings of digits. A credit-card number really isn't treated as a number--it's not an integer that is ever used in calculations, as far as I know. The individual digits are used, but not the entire number. The biggest problem is that when you have a string of digits, the string could contain leading 0's, which are lost if you treat it as one number. Maybe this isn't possible for CC numbers, but it's definitely possible for zip codes, and I've seen this problem in other situations.
@ajb +1 that is what people use in real time. I edited the answer accordingly. But so far the question was concerned, it was just to demonstrate the way dynamic array is constructed, taking user input as size. Thanks!
6

First get the input, then get the length, then build the array. Something like

//get input
System.out.print("Enter 15 or 16-digit credit card number: ");
String cardStr = input.nextLine();
long [] cc_num = new long[cardStr.length()];

6 Comments

For me, int[] cc_num = new int[cardStr.length()]; makes more sense. Because cc_num in itself is an array of 16 digits. Had it been something like holding cc num directly into a variable (non array), then long would fit. What is your opinion Elliott?
@JavaHopper Yes, using int[] (or even byte[]), or perhaps cardStr.toCharArray() would all be possible memory usage optimizations.
@ElliottFrisch Would it be better for me to hold the cc_num in a variable (non array) and then transfer each digit into an array position?
I don't find the need to hold it in array. What need do you see here?
@ziggy That sounds like a different question. I would use the toCharArray() method (myself) when implementing Luhn's algorithm.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.