Can someone help me fix this java program? I cant find errors but its not running
I recently started to learn Java, and I was giving the following problem to solve. After working on the code for a few days and eliminating the errors, my program still doesnt work even though there is no visible errors. Please help me. I have to turn this in pretty soon and I have tried everything but i cannot figure out what is the problem.
Task:
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 digit in the even places 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.
Here is my code:
Code Java:
import java.util.Scanner;
public class Taskone {
// Main method
public static void main(String[] args) {
// Scanner creation
Scanner input = new Scanner(System.in);
// User input and array size
System.out.println("\n\nEnter the number of your credit card: ");
long card_number= input.nextLong();
int size= 16;
// Array creation
long[] array = new long[size];
// Storing card number as array
for (int i = 0; i < array.length; i++)
{
System.out.println("\n"+i);
long temp = card_number;
while(temp !=0)
{
int temp_one = (int)temp%10;
array[i] = temp_one;
temp=temp/10;
}
System.out.println(array[i]);
}
/*
String output = "The salaries entered are: ";
for (int i = 0; i < array.length; i++)
{
output += array[i] + " ";
}
System.out.println(output);
*/
if (array[15] == 4)
{
System.out.println("\nYour credit card is a Visa Card.");
}
else if (array[15] == 5)
{
System.out.println("\nYour credit card is a Master Card.");
}
else if (array[15] == 3 && array[14] == 7)
{
System.out.println("\nYour credit card is an American Express Card.");
}
else if (array[15] == 6)
{
System.out.println("\nYour credit card is a Discover Card.");
}
else
{
System.out.println("\nYour credit card is invalid.");
}
// Step One
long sum_even = 0;
for (int i = 0; i < array.length; i=i+2)
{
long temp_store = array[i];
long double_value = temp_store * 2;
if (double_value/10 == 0 )
{
sum_even = sum_even + double_value;
}
else
{
long sum_internal = 0;
while(double_value/10 != 0)
{
long temp_two = double_value%10;
sum_internal = sum_internal + temp_two;
}
sum_even = sum_even + sum_internal;
}
}
// Step Two
long sum_odd = 0;
for (int i = 0; i < array.length; i=i+2)
{
long temp_store_one = array[i];
sum_odd = sum_odd + temp_store_one;
}
// Validation
long sum_total = sum_odd + sum_even;
if (sum_total%10 == 0)
{
System.out.println("\nYour credit card is valid.");
}
else
{
System.out.println("\nYour credit card is invalid.");
}
}
}
I would really appriciate any help. I used to work on note pad but now im using Eclipse.
Re: Can someone help me fix this java program? I cant find errors but its not running
The code works fine for me :P
you say you are using eclipse... a console window won't pop up, check the bottom panel in the eclipse window ;)
http://i427.photobucket.com/albums/p...54/ItWorks.png
Re: Can someone help me fix this java program? I cant find errors but its not running
Well if you noticed the numbers from 0 to 15 stand for array slots and the number under them stands for the number stored. As you can see, for some reason, the program just stores the last digit of the number entered in all of the array instead of the complete number, thus not working.
P.S. I know what a console is. I get the same thing. The problem is in that the number entered is not stored correctly in the array as you can see.
Re: Can someone help me fix this java program? I cant find errors but its not running
1. The while loop seems unnecessary in the for loop that converts the number.
2. Check the scope of the temp variable in the the for loop converting the number.
3. Put parenthesis around temp%10 when casting to an int(returns odd results without them).
4. In the while loop in step 1, double_value is not getting divided(thus locking the program in that loop).
If you require more help let us know, Good Luck :)