Salve a tutti ragazzi ho un problema con il costrutto else if
Questo è ciò che devo realizzare :
Ho due problemi :codice:L'inserimento di un elemento può essere impedito a causa dei seguenti casi: 1. L'identificatore è già presente nel contenitore; 2. La stringa è nulla; 3. Il contenitore è pieno; 4. L'identificatore non è un intero positivo Anziché restituire un valore Boolean, il servizio restituirà una delle seguenti costanti intere, definite nella classe StringPool e rese pubbliche: SUCCESS se l'inserimento è avvenuto correttamente; KO_DUPLICATE_FOUND se si è verificato il caso 1; KO_NULL_STRING se si è verificato il caso 2; KO_FULL_POOL se si è verificato il caso 3. ID_NOT_VALID se si è verificato il caso 4.
1) se cerco d'inserire un elemento che è un " KO_DUPLICATE_FOUND" alla posizione successiva alla 0 il programma mi restituisce SUCCESS al posto di ko_duplicate_found
2) se tento di d'inserire un elemento nella prima posizione libera lo inserisce sempre alla prima posizione fregandosene del controllo di ko_duplicate_found.
codice:public class StringUtils { private StringUtils[] str; public StringUtils(int newId, String newString){ identifier = newId; nomeid = newString; } public StringUtils(int n){ str = new StringUtils[n]; } public StringUtils(StringUtils[] d){ str = d; } public int getID(){ return identifier; } public String getNomeID() { return nomeid; } public int add(int id, String word){ int n = str.length; int statment = 0; for(int i = 0; i < n; i++){ if(str[i].getID() == id){ System.out.println("duplicate found"); return statment = KO_DUPLICATE_FOUND; } else if(word == ""){ System.out.println("null string"); return statment = KO_NULL_STRING; } else if(str[i] == null && i < n){ System.out.println("full pool"); return statment = KO_FULL_POOL; } else if(id < 0 ){ System.out.println("id non valido"); return statment = ID_NOT_VALID; } else{ str[i] = new StringUtils(id,word); System.out.println("STRING_POOL_SUCCES Code Return : " + STRING_POOL_SUCCESS); return statment = STRING_POOL_SUCCESS;} } return statment; } /** * recupera il nome di una stringa in base all'id. * @param recupero l'id da inserire */ public void recuperoStringa(int recupero) { int j = 0; int n = str.length; int posizione = -1; while(( j < n ) && posizione == -1 ) { if(str[j].getID() == recupero) posizione = j; j++; } System.out.println(str[posizione].getNomeID()); } private int identifier; private String nomeid; //rimozione o inserimento avvenuto con successo final int STRING_POOL_SUCCESS = 0; //identificatore già presente nel contenitore final int KO_DUPLICATE_FOUND = 1; //la stringa è nulla final int KO_NULL_STRING = 2; //il contenitore è pieno final int KO_FULL_POOL = 3; //l'identificatore non è un intero positivo final int ID_NOT_VALID = 4;

Rispondi quotando