Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ERROR WHEN I COMPILE. int val = Integer.parseInt(start2Chars); //CREDITCARD.JAVA

ID: 3836752 • Letter: E

Question

ERROR WHEN I COMPILE. int val = Integer.parseInt(start2Chars); //CREDITCARD.JAVA

card.Issuer(Cards[i])); //CREDITCARDEVALUTION

HELP FIX THE ERROR

//CreditCard.java /** * Write a description of class CreditCard here. * * @author (your name) * @version (a version number or a date) */ public class CreditCard { /** * @param cards * @return */ public boolean isValidCard(String cards) { int val[] = new int[cards.length()]; for (int i = 0; i < cards.length(); i++) { val[i] = Integer.parseInt(cards.charAt(i) + ""); } int x = LastDigitDrop(val); int reverse[] = DigitsReverse(val); int multiplyOdd[] = OddDigitsdouble(reverse); int addDigits[] = DigitsAdd(multiplyOdd); int sum = 0; for (int i = 0; i < addDigits.length; i++) sum += addDigits[i]; boolean validate = valid(sum, x); if (validate) return true; else return false; } /** * @param cardNumber * @return */ public int LastDigitDrop(int[] cardNumber) { return cardNumber[cardNumber.length - 1]; } /** * @param cardNumber * @return */ public int[] DigitsReverse(int[] cardNumber) { int revrs[] = new int[cardNumber.length]; for (int i = 0; i < cardNumber.length; i++) { revrs[i] = cardNumber[cardNumber.length - 1 - i]; } return revrs; } /** * @param revrs * @return */ public int[] OddDigitsdouble(int[] revrs) { int Odds[] = new int[revrs.length]; for (int i = 0; i < revrs.length; i++) { if (i % 2 == 0) { Odds[i] = revrs[i] * 2; } else { Odds[i] = revrs[i]; } } return Odds; } /** * @param mulOdd * @return */ public int[] DigitsAdd(int[] mulOdd) { int sumDig[] = new int[mulOdd.length]; int first, second; for (int i = 0; i < mulOdd.length; i++) { if (mulOdd[i] >= 10) { first = mulOdd[i] / 10; second = mulOdd[i] % 10; sumDig[i] = first + second; } else if (mulOdd[i] < 10) { sumDig[i] = mulOdd[i]; } } return sumDig; } /** * @param s * @param x * @return */ public boolean valid(int s, int x) { if ((s + x) % 10 == 0) return true; else return false; } /** * @param cardNumber * @return */ public String Issuer(String cardNumber) { String start2Chars = cardNumber.substring(0, 2); int val = Integer.parseInt(start2Chars); if (val == 34 || val == 37) return "American Express"; else if (val == 45 || val == 44) return "VISA"; else if (val == 51 || val == 53) return "MasterCard"; else if (val == 60) return "Discover"; else if (val == 31 || val == 33) return "JCB"; else if (val == 54 || val == 55) return "Diners Club - North America"; else if (val == 30) return "Diners Club - Carte Blanche"; else if (val == 36) return "Diners Club - International"; else if (val == 58) return "Maestro"; else if (val == 67) return "LASER"; else if (val == 48 || val == 49) return "Visa Electron"; else if (val == 63) return "InstaPayment"; else return "Not issued by any"; } } //CreditCardEvaluation.java import java.io.File; import java.io.PrintWriter; import java.util.Scanner; /** * Write a description of class CreditCardEvaluation here. * * @author (your name) * @version (a version number or a date) */ public class CreditCardEvaluation { public static void main(String args[]) { CreditCard card = new CreditCard(); String Valid_numbers[] = new String[50]; String Invalid_numbers[] = new String[50]; String Cards[] = new String[50]; int V_count = 0, Inv_count = 0, count = 0; try { Scanner inp = new Scanner(new File("DataEval.txt")); while (inp.hasNextLine()) { Cards[count] = inp.nextLine(); count++; } inp.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println(" Card number Card Issuer"); for (int i = 0; i < count; i++) { if (Cards[i].length() >= 13 && Cards[i].length() <= 19) { System.out.printf("%20s %s ", Cards[i], card.Issuer(Cards[i])); if (card.isValidCard(Cards[i])) { Valid_numbers[V_count] = Cards[i]; V_count++; } else { Invalid_numbers[Inv_count] = Cards[i]; Inv_count++; } } else { System.out.println("Length of the card number is not valid."); } } try { PrintWriter vPrint = new PrintWriter(new File("valid_cards.txt")); for (int i = 0; i < V_count; i++) vPrint.println(Valid_numbers[i]); vPrint.flush(); vPrint.close(); PrintWriter invPrint = new PrintWriter(new File( "invalid_numbers.txt")); for (int i = 0; i < Inv_count; i++) invPrint.println(Invalid_numbers[i]); invPrint.flush(); invPrint.close(); } catch (Exception e) { e.printStackTrace(); } } } DataEval.txt 3158539628375348 3337465828248404 3112804683572030 3112804683572033 5435528978467581 6706465468107999 6304362971054667 6706169762827894 6706169762827892 4844104341377697 4913405490886727 4844885754351829 4844885754351822 6371473570875275 6381475006869978 6389057917814806 347100734345549 347100734345543 6011162533805000 6011621663574413 6011824617460743 6011824617460745 6389057917814802 4539318768050385 36880982786892 36845793345501 36661154562232 36661154562234 5893329089846994 6761680165952016 6763100400984029 6763100400984022 5127043299122389 5330838017737340 5429168755154025 5429168755154023 375354034606481 379570632133224 4485521241443135 4532916206508075 4532916206508076 5590976687287124 5540641137519895 5540641137519892 30522070708059 30066552673241 30365487186802 30365487186801 invalid_numbers.txt 3158539628375348 3337465828248404 3112804683572030 3112804683572033 5435528978467581 6706465468107999 6304362971054667 6706169762827894 6706169762827892 4844104341377697 4913405490886727 4844885754351829 4844885754351822 6371473570875275 6389057917814806 347100734345549 347100734345543 6011162533805000 6011621663574413 6011824617460743 6011824617460745 6389057917814802 4539318768050385 36880982786892 36845793345501 36661154562232 5893329089846994 6761680165952016 6763100400984029 5127043299122389 5330838017737340 5429168755154025 5429168755154023 375354034606481 4485521241443135 4532916206508075 4532916206508076 5590976687287124 5540641137519895 5540641137519892 30522070708059 30066552673241 30365487186802 30365487186801 valid_cards.txt 6381475006869978 36661154562234 6763100400984022 379570632133224

Explanation / Answer

Answer:

I checked your code. It is working fine. I think you are not properly maintaining the text files. Please check whether the text files are having only numbers, not texts. There will be three separate files - "DataEval.txt", "invalid_numbers.txt" and "valid_cards.txt". Check if the text files are maintained separately and check if only numbers are there. Check the Inputs below and also the Output-

DataEval.txt

3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6381475006869978
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
6011824617460743
6011824617460745
6389057917814802
4539318768050385
36880982786892
36845793345501
36661154562232
36661154562234
5893329089846994
6761680165952016
6763100400984029
6763100400984022
5127043299122389
5330838017737340
5429168755154025
5429168755154023
375354034606481
379570632133224
4485521241443135
4532916206508075
4532916206508076
5590976687287124
5540641137519895
5540641137519892
30522070708059
30066552673241
30365487186802
30365487186801

invalid_numbers.txt

3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
6011824617460743
6011824617460745
6389057917814802
4539318768050385
36880982786892
36845793345501
36661154562232
5893329089846994
6761680165952016
6763100400984029
5127043299122389
5330838017737340
5429168755154025
5429168755154023
375354034606481
4485521241443135
4532916206508075
4532916206508076
5590976687287124
5540641137519895
5540641137519892
30522070708059
30066552673241
30365487186802
30365487186801

valid_cards.txt

6381475006869978
36661154562234
6763100400984022
379570632133224

I maintained those text files properly and got the output like below -

   Card number        Card Issuer
3158539628375348    JCB
3337465828248404    JCB
3112804683572030    JCB
3112804683572033    JCB
5435528978467581    Diners Club - North America
6706465468107999    LASER
6304362971054667    InstaPayment
6706169762827894    LASER
6706169762827892    LASER
4844104341377697    Visa Electron
4913405490886727    Visa Electron
4844885754351829    Visa Electron
4844885754351822    Visa Electron
6371473570875275    InstaPayment
6381475006869978    InstaPayment
6389057917814806    InstaPayment
347100734345549    American Express
347100734345543    American Express
6011162533805000    Discover
6011621663574413    Discover
6011824617460743    Discover
6011824617460745    Discover
6389057917814802    InstaPayment
4539318768050385    VISA
36880982786892    Diners Club - International
36845793345501    Diners Club - International
36661154562232    Diners Club - International
36661154562234    Diners Club - International
5893329089846994    Maestro
6761680165952016    LASER
6763100400984029    LASER
6763100400984022    LASER
5127043299122389    MasterCard
5330838017737340    MasterCard
5429168755154025    Diners Club - North America
5429168755154023    Diners Club - North America
375354034606481    American Express
379570632133224    American Express
4485521241443135    VISA
4532916206508075    VISA
4532916206508076    VISA
5590976687287124    Diners Club - North America
5540641137519895    Diners Club - North America
5540641137519892    Diners Club - North America
30522070708059    Diners Club - Carte Blanche
30066552673241    Diners Club - Carte Blanche
30365487186802    Diners Club - Carte Blanche
30365487186801    Diners Club - Carte Blanche

Please let me know if you get the the right output now.