Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Can someone help me fix this java program? I cant find errors but its not running

1. ## 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.

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:
```
import java.util.Scanner;

// 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 == 4)
{
System.out.println("\nYour credit card is a Visa Card.");
}

else if (array == 5)
{
System.out.println("\nYour credit card is a Master Card.");
}

else if (array == 3 && array == 7)
{
System.out.println("\nYour credit card is an American Express Card.");
}

else if (array == 6)
{
System.out.println("\nYour credit card is a Discover Card.");
}

else
{
}

// 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)
{
}

else
{
}

}
}```

I would really appriciate any help. I used to work on note pad but now im using Eclipse.  Reply With Quote

3. ## Re: Can someone help me fix this java program? I cant find errors but its not running

The code works fine for me you say you are using eclipse... a console window won't pop up, check the bottom panel in the eclipse window    Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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   Reply With Quote