在for循环中是一个if语句。在if语句内是另一个for语句,这使我抛出错误-int无法转换为布尔值。我还将包括变量及其摘要。
我已经搞砸了,试图做不同的循环等等。我希望至少能够消除该错误,以便我可以继续进行调试。
其余代码:
private boolean validateCard()
{
int odd;
int even;
String input = creditCard.getText(); // Get the input from credit card text field.
tokensList = new ArrayList<String>(); // Create an array list to be able to add more 2 digit numbers as needed.
if (input.length() < 16)
cardValidation.setText("Please enter a card number that is 16 digits long");
digits = input.split(""); // Convert the string to an array of characters.
oddDigits = new int[8]; // Create an int array to hold input from the credit card numbers.
for (odd = 1; odd < digits.length; odd++)
{
doubleDigits = Integer.parseInt(digits[odd]);
doubleDigits *= 2;
oddDigits[reIndex] = doubleDigits;
oddArray[reIndex] = String.valueOf(oddDigits[reIndex]); // Populate a string array the values of the odd numbers.
odd++; // Increment the odd index so it will add the next odd index to the oddDigits variable.
reIndex++; // Increment the resuable index.
}
reIndex = 0; // Reset the Reusable index.
这是主要问题区域。
for (int i = 0; i < oddDigits.length; i++)
{
if (oddDigits[i] > 10 && oddDigits[i] < 100)
{
oddTokens = oddArray[i].split("");
int oddInteger;
if (oddTokens.length > 1)
{
int sum = 0;
for (int index = 0; oddTokens.length; index++)
{
sum += Integer.parseInt(oddTokens[index]);
}
tokensList.add(oddArray[i]);
}
}
}
变量:
public class Payment extends javax.swing.JFrame {
private boolean valid = false;
private ArrayList<String> tokensList;
private SellerInterface si;
private int[] sumTotal;
private int[] intTokens;
private int[] sumTokens;
private String[] oddDoubles;
private String[] digits; // Digits from the credit card number.
private int[] oddDigits;
private int reIndex = 0; // Reusable Index
private int doubleDigits; // integer for double digits of the credit card values
private int[] evenDigits; // integer for the even digits of the credit card number values
private String[] oddArray; // Array of the odd numbers on the credit card.
private String[] evenArray; // Array of the even numbers on the credit card.
private String[] oddTokens; // Will convert each double digit number to a singular number by adding the two digits together (split method).
我希望它能够让我循环执行for循环,并将信用卡的2位数字的总和加起来(校验和算法)。我也希望能提供一些建议来简化此代码(在这一点上有点令人头疼)。
错误消息: 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException