Write a program that asks the user to enter an account number as a string. The account number consists of 8 characters and is divided as follows: The first 2 characters are digits and represent the branch (00 for Beirut, 01 for Saida, 02 for Tripoli). The remaining 6 characters represent the customer’s account number A valid account number consists of exactly 8 characters. If the account is valid, the program prints the details (branch and account number). If the account is not valid, the program displays a message to the user.
import java.util.Scanner;
public class AccountValidator {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String account="";
System.out.println("Please Enter your account nubmer (first two digit should be the code: 00 for Beirut, 01 for Saida, 02 for Tripoli) and press Enter:");
account=scan.nextLine();
char array[]=account.toCharArray();
for (int i = 0; i < array.length; i++) {
if(array[0]!='0') {
System.out.println("Invalid input:!!!First symbol must be 0.Try again");
}else if(!(Character.isDigit(array[i]))) {
System.out.println("Invalid input:!!!Symbols should be digits.Try again");
}else if(array.length!=8) {
System.out.println("Invalid input:!!!Symbols should be 8.Try again");
}
}
scan.close();
}
}
Comments
Leave a comment