Thanks for taking the time to open this. I am just starting to learn Java and cannot seem to get past a point in the program I'm currently working on. I'm making a credit card verifier, where you enter the numbers and an algorithm deems it valid or invalid. I have that part down, but I cannot figure out how to adjust when given a different length credit card.

Let me explain further: The program has to be able to accept cards between 13 an 16 digits in length. I'm currently using an array (which I know cannot be adjusted once created) to perform my algorithm. This accepts and stores 16 numbers and then performs what I need it to. I need to somehow allow my user to input a number or character that sets the array to the length of what they have already entered. RIght now, if they stop after 14, my program is still looking for numbers until it hits 16. I need the user to be able to stop the input and for the program to be able to adjust for the input (since it depends on the indexes of the input to verify it)

This is what I have so far, the algorithm isn't imposed yet. However, you do see that I am trying to use the numbers.length command to get the index of the last number (whether that is 13, 14, etc.) and multiply it by 2. Is this acceptable?

Any help is appreciated.

import java.util.Scanner;
import java.util.Arrays;


public class Exercise05 {
	public static void main(String []args) {
	
	Scanner input= new Scanner(System.in);
	
	int i;
	int[] numbers;
	double round1;
	
	numbers= new int [16];
	
	
	for(i=0 ;i < numbers.length; i++){
	
	numbers[i]=input.nextInt();
	}
	
	System.out.print("The Credit Card digits you entered are: ");
	System.out.println(Arrays.toString(numbers));
	
	
	double round1= (numbers[numbers.length] * 2);
	
	System.out.println( "you " + round1 );
		
	
	if( numbers[0]==4)
		System.out.println("This is a Visa");
		
		else if( numbers[0]==5)
			System.out.println("This is a Mastercard");
			
		else if( numbers[0]==6)
			System.out.println("This is a Discover Card");
			
		else if( numbers[0]==3 && numbers[1]==7 )
			System.out.println("This is a Mastercard");
			
		else
			System.out.println("Invalid card");
			
			
	
	}
}

You can initialize your array to contain -1's and then just ignore them when you are reading the cards, or you can use ArrayList which is a mutable array.

Thank you for your reply.


Could you explain further? I don't understand how to use -1's just yet and we are not allowed to use ArrayList.

Thank you for your reply.


Could you explain further? I don't understand how to use -1's just yet and we are not allowed to use ArrayList.

You can initiate all values in an array to -1 by doing:

int arr[10] = {-1};

I looked more closely at your code and it looks like you are only checking the first digit of the card, and your error should be caused by

numbers[i]=input.nextInt();

So instead of your for loop checking for the length of numbers.length, you should be checking input's length. You can even check the length first, then create your array that's exactly that size. Hope this helped.

you can store it in a String,instead of an array. you can easily get the length and character on a certain place (based on the index) of a String.

the code you have now, will always continue until 16 because you tell it to.
read in one String, verify the length, check for characters that are not digits and filter those out
(for instance: if you would have entered
55586232 as 555-862-32)

afterwards, if you really want to, you can always convert the String to an array, if that makes you feal more comfortable with that.

I don't have a problem with the bottom half of the code, I'm sorry. I simply want a way to create the length of my array based on what a user enters. Here is another thing I am working on to accomplish that:

import java.util.Scanner;
import java.util.Arrays;


public class sample {
	public static void main(String []args) {
	
	Scanner input= new Scanner(System.in);
	
	int i;
	int[] numbers;
	
	
	System.out.println("How many digits is your card?");
	
		
			if (input.nextInt()==16)
				numbers= new int [15];
			else if (input.nextInt()==15) 
				numbers= new int [14];
			else if (input.nextInt()==14)
				numbers= new int [13];
			else if (input.nextInt()==13)
				numbers= new int [12];
			
			
				
				
System.out.println("Enter your digits:");

	for(i=0 ;i < numbers.length; i++){
	
	numbers[i]=input.nextInt();
	}

It's not working though. Very frustrating since I think this is a simple problem.

Thank you, Stultuske. I'll look into that and see what I can do.

well ... yes, your length has to be the same as the number of digits. it just starts with index 0, that's the difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.