User Controls

Rate my Java programming skills

  1. #1
    SBTlauien African Astronaut
    This is a short little program I wrote that'll find and print out all valid(passing the Luhn algorithm) credit card numbers within a range. I didn't use any existing code that's out there(although I knew I could have). Did I go about it in the best way? Does the code look okay? What could I have done differently?


    import java.util.ArrayList;

    public class Nir {

    public static ArrayList<int[]> numberSet;
    public static String number1 = "4358806222700000";
    public static String number2 = "4358806222709999";

    public static void main(String[] args) throws Exception {
    numberSet = new ArrayList<int[]>();
    int[] a = new int[16];
    int[] b = new int[16];
    for (int i = 15; i > -1; i--){
    a[i] = number1.charAt(i) - 48;
    b[i] = number2.charAt(i) - 48;
    }
    createList(a, b);
    for (int n = 0; n < numberSet.size(); n++){
    int[] temp = numberSet.get(n);
    for (int i = 0; i < 16; i++){
    System.out.print(temp[i]);
    }
    System.out.println();
    }
    }

    public static void createList(int[] range1, int[] range2){
    while(!equals(range1, range2)){
    if (isValid(range1)){
    int[] temp = new int[16];
    for (int i = 0; i < 16; i++){
    temp[i] = range1[i];
    }
    numberSet.add(temp);
    }
    range1 = increment(range1);
    }
    }

    public static boolean isValid(int[] n){
    int total = 0;
    for (int i = 15; i > -1; i--){
    if (i % 2 != 0){
    total = total + n[i];
    } else {
    Integer temp = n[i] + n[i];
    int tempt = 0;
    for (int x = 0; x < temp.toString().length(); x++){
    tempt = tempt + Integer.parseInt(String.valueOf(temp.toString().charAt(x)));
    }
    total = total + tempt;
    }
    }
    if (total % 10 == 0) {
    return true;
    } else {
    return false;
    }
    }

    public static int[] increment(int[] i){
    int[] returner = i;
    boolean finished = false;
    int s = 15;
    while (!finished){
    if (i[s] != 9){
    returner[s]++;
    finished = true;
    } else {
    returner[s] = 0;
    s--;
    }
    }
    return returner;
    }

    public static boolean equals(int[] range1, int[] range2){
    for (int i = 0; i < 16; i++){
    if (range1[i] != range2[i]){
    return false;
    }
    }
    return true;
    }

    }
  2. #2
    Sophie Pedophile Tech Support
    I don't even Java but it looks pretty clean to me.
Jump to Top