Check character and value repeated in array of strings

1

I have an array of strings

 String[] jt = { "João Mendonça", "Mário Andrade", "João Mendonça"});

What you are supposed to do is verify if the array contains at least one character and has no repeated name , or in this specific case it contains a letter in min and has a repeated name. the expected output will be:

java.lang.IllegalArgumentException: nome repetido

My problem is how to check if you have at least one letter character and how to check if it has a repeated name.

Explaining better:

I have String Array of JT to which I want to do 2 checks:

1- Check if there is at least 1 character that is letter.

2 - Check if there are equal names: jt= {"nome","nome","nome") , if there is a return

java.lang.IllegalArgumentException: nome repetido
    
asked by anonymous 30.04.2014 / 15:01

2 answers

2

You can first make a comparison of each string and check if there is any more record with this value your example . Or you can do it differently than I remembered now:

List jtLista = Arrays.asList(jt); //Lista com todos os elementos
Set jtSet = new HashSet(jtList);  //Lista sem elementos repetidos
if(jtSet.size()< jtList.size())
    //tem repetidos

Then to check if you have at least one character you can do this:

//compila a expressão regular com o alfabeto em maiúsculas e minúsculas
Pattern p = Pattern.compile("[a-zA-Z]");  
// faz o match da string "AB 45"  com a expressão regular.
Matcher m = p.matcher("AB 45");         

//se existirem resultados é porque houve match, ou seja, contém letras.
if(m.find())  
  System.out.println("A string contem letras");
    
30.04.2014 / 15:17
0

The part of the repeated I solved thus (with the help of a user that posted here):

int i = 0;
    int j = 0;
    boolean find = false;
    while (i < autores.length && find == false) {
        j++;
        while (j < autores.length && find == false) {
            find = autores[i].equals(autores[j]);
            j++;
        }
        i++;
    }
    if (find == true) {
        throw new IllegalArgumentException("Existem autores repetidos"); 
    } else {
        getAutores();

    }   
    
30.04.2014 / 17:39