I have a string input and I'm trying to convert it to a character array then store the numbers from that array in a manner that will let me use them in a credit card program. This is how i am currently trying to do it.
I'm currently getting Null Pointer Exception at :
charDigits[x - invalid] = charInput[x];
I need to be able to use the integers on the credit card algorithm from a string input.
public class testFunction {
private char[] charInput;
private char[] charDigits;
private int[] intInput;
private int invalid = 0;
private String input = "125-6543-3356";
public testFunction()
{
charInput = input.toCharArray();
for(int x = 0; x < charInput.length; x++)
{
if (charInput[x] == '0' || charInput[x] == '1' || charInput[x] == '2' || charInput[x] == '3' || charInput[x] == '9' ||
charInput[x] == '4' || charInput[x] == '5' || charInput[x] == '6' || charInput[x] == '7' || charInput[x] == '8')
{
charDigits[x - invalid] = charInput[x];
}
else
{
invalid++;
}
System.out.println("charDigits: " + x + ": " + charDigits[x] );
}
for(int i = 0; i < charDigits.length; i++)
{
intInput[i] = Integer.parseInt(charDigits[i] + "");
}
}
public static void main(String[] args)
{
testFunction test = new testFunction();
}
}
if(c >= '0' && c <= '9')
rather than chaining all those||
together. As you can see here the code points for0
to9
are contiguous. – NameSpace Mar 23 at 22:11