Sunteți pe pagina 1din 2

/*Crihan Liviu Cristian 2031 (+Vlad Horatau)

* 1. Write a Java application which defines an authentication key with


* the format: XXXXX-XXXXX-XXXXX-XXXXX, where X is a character which can
* be either a digit or a letter. The application should verify if this
* key has exactly 4 groups of characters with 5 characters each, and
* separated by the symbol -. Also, compute the number of digits and
* letters from the authetication key. The number of digits should be greater
* than the number of letters, and the number of letters cannot be 0. If any
* of the above conditions are not met, display the message: Invalid
authetication key!
*/

import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.String;

public class key {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


String k1 = null;
System.out.print("Enter the key: ");
k1 = scanner.nextLine();
//Taking the key
int charCount = 0;
int digiCount = 0;
int counter=0;

StringTokenizer st = new StringTokenizer(k1,"-");

while ( st.hasMoreTokens() ){

String token = (String)st.nextToken();


if(token.length()==5){
char[] key=token.toCharArray();

for(int i=0;i<token.length();i++)
{
char temp = token.charAt( i );

if( Character.isLetter(temp) )
charCount++;
}
for(int i=0;i<token.length();i++)
{
char temp = token.charAt( i );

if( Character.isDigit(temp) )
digiCount++;
}
}
else System.out.println("Invalid authetication key!");

counter++;
//Verify each token for length and counts digits, characters,
and number of tokens.

}
if(counter==4) {
if(digiCount>charCount){
if(charCount!=0)
System.out.println("Correct key!");
}
else System.out.println("Invalid authetication key!");
}

}
}
//Compares the counters to the requirements

S-ar putea să vă placă și