Check repeated character and value in string array

Asked

Viewed 4,662 times

1

I have an array of strings

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

What’s supposed to be check out if the array contains at least one letter character and that has no repeated name, that is, in this specific case it contains in min a letter and has a repeated name. the expected output will be:

java.lang.IllegalArgumentException: nome repetido

my problem is how to check if it has at least one letter character and how to check if it has repeated name.

Explaining better:

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

1-check that at least 1 character is a letter.

2 - Check for equal names: jt= {"nome","nome","nome"), if there exists returns

java.lang.IllegalArgumentException: nome repetido
  • Voce could implement the code of eliminate repeated values switching from int to string right?

  • is talking about the repetition, is what I imagined but does not work even changing ints to strings

  • 1

    because it doesn’t work? what went wrong?

  • @board11 Have a repeated name where?

  • 1

    my mistake, I’ve already edited

2 answers

2

First you can make a comparison of each string and check if there is any other record with that value your example. Or you can do it another way that I just remembered:

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 it has at least one character you can do so:

//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");
  • I’m sorry, I don’t quite understand, can you explain your code better?

  • Just new around here. 'Cause it’s broken?

  • @Math the other answer was right here and was deleted. I will remove the link.

  • was thinking more like: for (int k=0; k< authors.length; k+){ if (authors[k].contains("[a-z]"){ // getAuthors(); System.out.print("contains letters"); } } logical that does not work

  • @board11 I have already completed the answer. It has an easier way to see if it has repeats. I don’t know which is the best way, but it gets the idea.

0

Repeat part I solved so(with the help of a user who 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();

    }   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.