# Credit card validation

Page 1 of 1

## 5 Replies - 10417 Views - Last Post: 28 September 2010 - 04:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192382&amp;s=254e3ee0a39e3b5838b076c3529685ce&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 huss_584

Reputation: -3
• Posts: 26
• Joined: 06-September 10

# Credit card validation

Posted 27 September 2010 - 09:19 PM

pleas any one help me to biult this code :

Credit card numbers follow certain pattern. A credit card number must be 16 digits, and must start with
• 4 for Visa cards
• 5 for Master cards
• 37 for American Express cards
• 6 for Discover cards
Suppose that you were asked to write down a Java program that validates the credit card number (if it is correct or not!). Here are the rules that determine the correctness of credit card number:
Suppose the credit card number = 4388576018402625
1) Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single digit number:
2*2 = 4
2*2 = 4
4*2 = 8
1*2 = 2
6*2 = 12 (1 + 2) = 3
5*2 = 10 (1 + 0) = 1
8*2 = 16 (1 +6) = 7
4*2 = 8

2) Now add all single-digit number from step 1 (4+4+8+2+3+1+7+8 = 37)

3) Add all digits in the odd places from right to left in the card number:
5+6+0+8+0+7+8+3 = 37

4) Sum the results from step 2 and 3
37 + 37 = 74

5) If the result in step 4 is divisible by 10, then credit card is valid, else it is not valid.
74 is not divisible by 10, so it is not valid!
Write a java program to accept a credit card number from keyboard as long, and then prints on screen, if it is valid or not. You should use the following:
public static int[] fillArray(long) : this method fills the credit card number in an array
public static int getSum(int[]) :calculated the summation from step 1 and 2
Public static int getOddSum(int[]): calculates the summation from step 3
Public static boolean isValid(int, int): determines if valid or not according to step 5.
without usin object oriented

This post has been edited by pbl: 28 September 2010 - 04:25 PM
Reason for edit:: Topic title changed to be more meaningful

Is This A Good Question/Topic? 0

## Replies To: Credit card validation

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Credit card validation

Posted 27 September 2010 - 09:30 PM

First respect rule #4 of this forum
Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"
Second rule #1 and #2
We will not do your homework for you! Do not ask us to give you code!
Use BBCode when posting. For example:
```Paste Your Code Here
```

All these rules were showed to you when you posted
Rule #2 is important because
[rules][/rules]

### #3 huss_584

Reputation: -3
• Posts: 26
• Joined: 06-September 10

## Re: Credit card validation

Posted 28 September 2010 - 03:20 PM

How can i fill long intger in array

This post has been edited by huss_584: 28 September 2010 - 03:22 PM

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,154
• Joined: 27-December 08

## Re: Credit card validation

Posted 28 September 2010 - 03:30 PM

Down cast it to an int. Note that if the long exceeds the capacity of an int, it will overflow. In fact, when downcast, Long.MAX_VALUE is equivelant to -1 as an int.
```long y = 12345L;
x[someIndex] = (int)y;

```

This post has been edited by macosxnerd101: 28 September 2010 - 04:17 PM
Reason for edit:: Fixed variable names. Thanks pbl!

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Credit card validation

Posted 28 September 2010 - 04:03 PM

Too many x becomes confusing and wont even compile
```int[] y = new int[10];
long x = 12345L;
y[someIndex] = (int)x;

```

But really don't see where you would need to cast long to int to fix your problem

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,154
• Joined: 27-December 08

## Re: Credit card validation

Posted 28 September 2010 - 04:22 PM

Thanks for catching that typo! Fixed.

As for the casting, as long is of greater magnitude than int, you must explicitly downcast from long to int to store in an int[] (see the requirements). Otherwise, you will get a compilation error.