0

How can I add a statement that allows me to check if the credit card number inputted by the user is a palindrome? I am checking for the appropriate length already so how can i Input the new palindrome checker into this code:

import java.util.Scanner;

public class DT18 {
    public static void main(String[] args) {
        String number;
        Boolean debug = false;

        if (args.length == 0) { // no command line
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Please enter a Credit Card number to validate.");
            number = keyboard.next();
        } else { // command line input
            number = args[0];
        }


        if (debug) System.out.println("String Length " + number.length());


        if (number.length() < 10) {
            System.out.println("Not Valid");
        }


        int sum = 0;
        int oddDigit = 0;
        for (int i = number.length() - 1; i >= 0; i--) {
            if (debug) System.out.println("i = " + i);
            if ((Character.getNumericValue(number.charAt(i)) < 0) || (Character.getNumericValue(number.charAt(i)) > 9)) {
                System.out.println("Not Valid");
                break;
            }
            if (i % 2 == 0) { //Even Digit
                sum += Character.getNumericValue(number.charAt(i));
            } else { //Odd Digit
                oddDigit = (2 * Character.getNumericValue(number.charAt(i)));
                if (oddDigit > 9) oddDigit = (oddDigit % 10) + 1;
                sum += oddDigit;
            }
            if (debug) System.out.println(sum);
        }
        if (sum % 10 == 0) {
            System.out.println("Valid");
        } else {
            System.out.println("Not Valid");
        }

    }

}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
  • 3
    What does a palindrome have to do with Credit Card validation? Also, this question needs better tags. At the very least, add a Java tag and maybe something about input or credit card validation. – Will May 06 '15 at 04:07
  • Just going the extra mile in this code. Trying to add something cool haha – Dante Messi Tapia May 06 '15 at 04:14
  • possible duplicate of [Check string for palindrome](http://stackoverflow.com/questions/4138827/check-string-for-palindrome) – Tom May 06 '15 at 10:32

4 Answers4

2

From an answer I once gave here:

public boolean isPalindrom(int n) {
    return new StringBuilder("" + n).reverse().toString().equals("" + n);
}
Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

This post should give you for loop logic:

http://www.programmingsimplified.com/java/source-code/java-program-check-palindrome

   public static void main(String args[])
   {
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Enter a string to check if it is a palindrome");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1; i >= 0; i-- )
     reverse = reverse + original.charAt(i);

  if (original.equals(reverse))
     System.out.println("Entered string is a palindrome.");
  else
     System.out.println("Entered string is not a palindrome.");
}
Grady G Cooper
  • 1,044
  • 8
  • 19
0

You can write a simple function to check if a string is a palindrome or not.

 private static boolean checkPalindrome(String input) {
    int i = 0, j = input.length() - 1;
    for (; i < j; i++) {
        if (i == j) {
            return true;
        }
        if (input.charAt(i) == input.charAt(j)) {
            j--;
        }
        else
            return false;
    }
    return true;
}

This is a crude method; you may want to modify it according to your requirement, but it will get the job done in most of the cases.

saarthak gupta
  • 173
  • 1
  • 11
0

I've looked over the other answers and all of them have bad performance and working with String instead of just using the given number. So I'll add the version without conversion to String:

public static boolean isPalindrome(int n) {
    int[] digits = new int[length(n)];

    for (int i = 0; n != 0; ++i) {
        digits[i] = n % 10;
        n /= 10;
    }

    for (int i = 0; i < digits.length / 2; ++i) {
        if (digits[i] != digits[digits.length - i - 1]) {
            return false;
        }
    }
    return true;
}

public static int length(int n) {
    int len = 0;
    while (n != 0) {
        ++len;
        n /= 10;
    }
    return len;
}

Not sure, if that's the best implementation, but I got rid of Strings :-)

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48