Your algorithm has some little problems. The first is the following:
String [] lista1 = new String [];
When you create the array, you must specify its length. Something like this:
String[] lista1 = new String[10];
However, you can say that 10 is too much... or maybe too little... or maybe you should ask the user how many there are. By the way, you’re not asking the user to type anything, you’re just writing on the console without reading anything from there.
So, to read a console number:
System.out.print("Escreva quantos nomes há na primeira lista: ");
int tamanhoLista1 = sc.nextInt();
With this you can create the arrays:
String[] lista1 = new String[tamanhoLista1];
And then, you do the same with list 2. The statement of your exercise does not make it clear whether the two lists necessarily have the same size or not. But if they don’t, you can make the list size 1 different from the list size 2.
Missing also read the names. To read a name, you can do this:
String nome = sc.nextLine();
Or already read straight into the array:
nome[i] = sc.nextLine();
And in the case up there, you might want to know where this comes from i
. The answer is that you will need to read each of the lists of names using a for
and this i
shall be the index variable of that for
.
When it comes to comparing the lists, you are comparing the element 0 of a list with the element 0 of another list. The 1 of a list with the 1 of the other list. The 2 of a list with the 2 of the other list... But that’s not what you want! For example, imagine this case:
Lista1: Alberto, Fernanda, Marcelo, Rodrigo, Maria, Tatiana
Lista2: Joaquim, Carlos, Maria, Adriana, Fernanda, Marcelo, Alberto
How many names on the list 1 are on the list 2? The correct answer is 4 (Alberto, Fernanda, Marcelo e Maria). But your algorithm won’t find any of those names, because their positions don’t match.
To resolve this, I recommend taking a different approach. You will need to create two bonds for
One inside the other. The one from the outside going through the list 1 and the one from within going through the list 2, in order to compare all the elements of a list with all the elements of the other list. When you find an element that matches, you do the cont++;
and gives a break;
. The break;
is important, because once the list 1 element is found, you don’t want to keep looking (and if it continues, you will have problems with lists that have repeated elements).
Finally, one last detail:
return cont/lista2.length;
Here cont
is whole and lista2.length
is also integer. Thus, an entire division will be performed. Only after the entire division has been performed will the value be converted to an double
to be returned. That’s not what you want either, but the solution is easy:
return ((double) cont) / lista2.length;
This will give you a number between 0 and 1. As you want percentage, multiply by 100:
return ((double) (cont * 100)) / lista2.length;
What doubt are you having?
– user28595
It is giving problems in the execution of the Algorithm. At the time of putting the number of names. I wanted to put a while(hasNext()), but I don’t know how to put it, and I don’t know where. ?
– Android Programmer
What problem? Be more specific where it is occurring and what problem is occurring.
– user28595
Please, can you help me to run this Algorithm? It is giving problems that is missing the dimension of the Array. I wanted to put while(hasNext()). And even if you put the dimensions of the Lists, it gives the: java.lang.nullPointerException
– Android Programmer
The error already gave you the solution, inform the dimension of the vector when you start it. Ex.:
String[] meuVetor = new String[6]
where 6 is the dimension.– user28595
Even by placing the dimensions, it gives the error of java.lang.nullPointerException. Please, you can help?
– Android Programmer
If you also help me by putting exactly the stack of errors and on which line the error occurs in the question, yes.
– user28595
Right. The IDE says it gives error on this line: if (list1[i].equals(Lista2[i])) { E gives error on this line: System.out.println (calculatedList (list1, Lista2)); Java.lang.nullPointerException error
– Android Programmer
You have not filled in any information, hence the null error. When the loop goes through the indexes, all are null.
– user28595
I put: String [] list1 = new String [3]; E String [] list1 = new String [5]; Is this what you mean? If so, it gives exactly this error.
– Android Programmer
At what point in the code do you use the scanner to fill the lists? In the code you are not filling either of the two lists, you are just starting them.
– user28595
Right, and how to use the Arrays Scanner?
– Android Programmer
Makes a
for
iterating at each position of the arrays after instantiated and scaled, and at each position of them you use this Scanner object that you initialize and do this:lista1[i] = sc.nextLine();
. And don’t forget to close the Scanner, with´sc.close()
.– Christian Felipe