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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
     
    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.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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


  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by d00mhammers; February 22nd, 2011 at 01:19 PM.

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default 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

Similar Threads

  1. Running servlet from java project
    By raj07 in forum Java Servlet
    Replies: 1
    Last Post: February 19th, 2011, 07:11 PM
  2. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  3. My program doesnt want to do its math point my errors out please
    By Redlight in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 01:56 PM
  4. Help with Cannot Find Symbol Variable errors
    By skboone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 10:52 AM
  5. Textpad Program Running Problems
    By Paddy in forum Java IDEs
    Replies: 4
    Last Post: January 27th, 2010, 09:14 PM