mercredi 30 mars 2016

Java - Need to fix a simple algorithm

This method test if the string 'chaine' contain only (ABCDE... abcd...) or only number ('0' a '9'). OR can contain caracther that are in the string 'plus'

If the string chaine is empty or null it return false. If the string plus is empty or null it doesnt a matter.

@param chaine
@param plus
@return True if the string chaine contain only letter or number OR a caracther in the string plus

Expected EXAMPLE:

param : chaine = null,                plus = null         return false  Iget: false
param : chaine = null,                plus = ""           return false  Iget: false
param : chaine = null,                plus = "?%t64*"     return false  Iget: false
param : chaine = "",                  plus = null         return false  Iget: false
param : chaine = "",                  plus = ""           return false  Iget: false
param : chaine = "",                  plus = "?%t64*"     return false  Iget: false
param : chaine = "aBNghy",            plus = "?%t64*"     return true   Iget: false
param : chaine = "897654999",         plus = "?%t64*"     return true   Iget: false
param : chaine = "1abb876BR",         plus = "?%t64*"     return true   Iget: false
param : chaine = "1aB&b876(B)R",      plus = "?%t64*"     return false   Iget: false 
param : chaine = "1aB?b876(B**R%",    plus = "?%t64*"     return false   Iget: true
param : chaine = "1aB?b876B**R%",     plus = "?%t64*"     return true    Iget: true
param : chaine = "?**?",              plus = "?%t64*"     return true    Iget: true
param : chaine = "?*",                plus = ""           return false   Iget: false
param : chaine = "aBcD12Ef8",         plus = ""           return true   Iget: true
param : chaine = "98003119",          plus = ""           return true   Iget: true
param : chaine = "aBcdefG",           plus = null.        return true   Iget: true


 NOTE:
 *    - This method MUST use these method: 
 *          - estUneLettre(...) (isALetter)
 *          - estUnCarNum(...)  (isANumber)

public static boolean estUneLettre (char car) {
    return (car >= 'a' && car <= 'z') || (car >= 'A' && car <= 'Z');
}
public static boolean estUnCarNum (char car) {
    return (car >= '0' && car <= '9');
}     

My code that I need to fix:

    public static boolean estAlphaNumPlus (String chaine, String plus) {

    boolean chainePlus = false;
    String chaineNew;

    if (chaine == null || chaine.isEmpty()) {
        chainePlus = false;

    } else if (plus == null || plus.isEmpty()) { 

        for (int i = 0 ; i < chaine.length(); i++) {
            if (estUneLettre (chaine.charAt(i)) || estUnCarNum (chaine.charAt(i))) {
                chainePlus = true;

            }
        }

    } else {
        for (int j = 0 ; j < plus.length(); j++) {
            if (!estUnCarNum(plus.charAt(j)) && !estUneLettre(plus.charAt(j))) {
                chaineNew = plus.charAt(j) + "";

                if (chaine.contains (chaineNew)) {
                    chainePlus = true;

                }
                chaineNew = "";
            }
        }
    }

    return chainePlus; 
}

Aucun commentaire:

Enregistrer un commentaire