I need help with this java code.
I need to validate the input from the user to include entries in the fields, that customer id is an integer and that the credit card number is valid. It must start with a 4, 5, or 6 and must be in the format xxxx-xxxx-xxxx-xxxx. Otherwise, you must tell the user that it is invalid and let him try to enter the number.
Please do not use Regex on this question.
However this is a java program.
Java Code below:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author saurabh
*/
public class Card {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter Card Number: ");
String cn = br.readLine();
int n = cn.length();
int flag = 0;
if(n==16 || n==19){
char[] c = cn.toCharArray();
int num = (int)c[0]-(int)'0';
if(num!=4 && num!=5 && num!=6){
System.out.println("Card number must start with 4, 5 or 6.");
flag = 1;
}
if(n==16){
for(int i=0;i<16;i++){
num = (int)c[i]-(int)'0';
if(num>=0 && num<=9){
//do nothing
}
else{
System.out.println("Invalid Input");
flag = 1;
break;
}
}
}
else{
for(int i=0;i<19;i++){
if((i==4 || i==9 || i==14)&& c[i]!='-'){
System.out.println("Invalid Input ");
flag = 1;
break;
}
else{
if(i==4 || i==9 || i==14)
continue;
num = (int)c[i]-(int)'0';
if(!(num>=0 && num<=9)){
System.out.println("Invalid Input");
flag = 1;
break;
}
}
}
}
if(flag==0)
System.out.println("Correct Input!!!");
}
else{
System.out.println("Invalid Input");
}
}
}
Output:



The code was written and tested in NetBeans IDE 8.2.
Please like,
:-)
Thank you for your support.
I need help with this java code. I need to validate the input from the user...