Hi. I have a program that I have to do for school, and I can't figure out how to finish it without getting any errors. I believe my code is not reading the creditCardNum variable that I have introduced. I keep getting a StringIndexOutOfBoundsException. The class accepts an order for cheesecakes and then prints an order summary. The part I am having trouble with is the credit card portion. My teacher doesn't want me to introduce creditCardNum as an instance variable in my CreditCard class. Here is my code:

CreditCard class:

public class CreditCard{ /** * Determines whether the number entered is valid * @param creditCardNum * * @return valid */ public boolean isCardValid(String creditCardNum) { boolean valid; String singleDigitPrefix = creditCardNum.substring(0, 1); String doubleDigitPrefix = creditCardNum.substring(0, 2); if ((creditCardNum.length() == 13 || creditCardNum.length() == 16) && singleDigitPrefix.equals("4")) { valid = true; } else if (creditCardNum.length() == 16 && (doubleDigitPrefix.equals("51") || doubleDigitPrefix.equals("52") || doubleDigitPrefix.equals("53") || doubleDigitPrefix.equals("54") || doubleDigitPrefix.equals("55"))) { valid = true; } else if (creditCardNum.length() == 15 && doubleDigitPrefix.equals("37")) { valid = true; } else { valid = false; } return valid; } /** * Determines the type of card * * @param creditCardNum * @return cardType */ public String getCardType(String creditCardNum) { String cardType = null; String singleDigitPrefix = creditCardNum.substring(0,1); String doubleDigitPrefix = creditCardNum.substring(0, 2); System.out.println(singleDigitPrefix); if ((creditCardNum.length() == 13 || creditCardNum.length() == 16) && singleDigitPrefix.equals("4")) { cardType = "Visa"; } else if (creditCardNum.length() == 16 && (doubleDigitPrefix.equals("51") || doubleDigitPrefix.equals("52") || doubleDigitPrefix.equals("53") || doubleDigitPrefix.equals("54") || doubleDigitPrefix.equals("55"))) { cardType = "MasterCard"; } else if (creditCardNum.length() == 15 && doubleDigitPrefix.equals("37")) { cardType = "American Express"; } else { } return cardType; } /** * Masks all but the last four digits of the credit card * * @param creditCardNum * @return formattedNum */ public String maskCardNumber(String creditCardNum) { String formattedNum = null; if (getCardType(creditCardNum).equals("Visa")) { if (creditCardNum.length() == 13) { formattedNum = "XXXXXXXXX" + creditCardNum.substring(8, 13); } else { formattedNum = "XXXXXXXXXXXX" + creditCardNum.substring(11, 16); } } else if (getCardType(creditCardNum).equals("MasterCard")) { formattedNum = "XXXXXXXXXXXX" + creditCardNum.substring(11, 16); } else if (getCardType(creditCardNum).equals("American Express")) { formattedNum = "XXXXXXXXXXX" + creditCardNum.substring(10, 15); } else; return formattedNum; }

And here are the two methods that I need to call for the program:

public String readCreditCard(CreditCard creditCard) { System.out.print("\nPlease enter your Credit Card Number: "); String creditCardNum = keyboard.next(); while (creditCard.isCardValid(creditCardNum) == false) { System.out.println("Invalid credit card number " + creditCardNum); System.out.println("Please try again."); System.out.print("\nPlease enter your Credit Card Number: "); creditCardNum = keyboard.next(); } return creditCardNum; }

and this is the method that doesn't seem to be working properly:

public void printCreditCard(CreditCard creditCard, String creditCardNum) { System.out.println(creditCard.getCardType(creditCa rdNum) + " " + creditCard.maskCardNumber(creditCardNum)); printThankYou(); }

And finally, here's what I have in the driver class to call these methods:

String creditCardNum = ""; inputOutput.printCreditCard(creditCard, creditCardNum);

I'm sorry that this is long and possibly dumb, but I'm really new to this and I've been racking my brain trying to figure out what's wrong with the code. Please help.

More...