Java Help: Arrays

-3

Exercise:

  

Make two lists of names in the form of arrays, compare how many names in List 1 are in List 2, and then calculate their percentage of the total percentage of people in List 2

Here is the code I've already done:

import java.util.Scanner;

public class CalourosDoadores {

    public static void main (String args []) {

        System.out.println ("Escreva os Nomes na Primeira Lista: ");

        Scanner sc = new Scanner (System.in);

        String [] lista1 = new String [];

        System.out.println ("Escreva os Nomes na Segunda Lista: ");

        String [] lista2 = new String [];


        System.out.println (calcularLista (lista1, lista2));
    }

        public static double calcularLista(String lista1[], String lista2[]) {

            int cont = 0;

            for (int i = 0; i<lista1.length;i++) {

                if (lista1[i].equals(lista2[i])) {

                    cont++;
                }

            }

            return cont/lista2.length;
        }
}

Can someone please help me finish?

    
asked by anonymous 10.07.2016 / 03:53

2 answers

3

Your algorithm has some problems. The first is as follows:

String [] lista1 = new String [];

When you create the array, you must specify the length of the array. Something like this:

String[] lista1 = new String[10];

However, you can say that 10 is a lot ... or maybe a little ... or maybe you should ask the user how many they are. Incidentally, you are not asking the user to type anything, you are just writing on the console without reading anything from there.

Thus, to read a number from the console:

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 so, 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 have not, you can make the size list 1 be different from the size of list 2.

The names are also missing. To read a name, you can do this:

String nome = sc.nextLine();

Or, read directly into the array:

nome[i] = sc.nextLine();

And in the case above, you might want to know where this i comes from. The answer is that you will need to read each of the name lists using a for and this i will be the index variable of that for .

Already when comparing the lists, you are comparing the element 0 of a list with 0 of another list. The 1 from a list with 1 from the other list. The 2 from a list with 2 from the other list ... But that's not what you want! For example, imagine this case:

  

List 1: Alberto, Fernanda, Marcelo, Rodrigo, Maria, Tatiana
  List 2: Joaquim, Carlos, Maria, Adriana, Fernanda, Marcelo, Alberto

How many names in list 1 are in list 2? The correct answer is 4 (Alberto, Fernanda, Marcelo and Maria). But your algorithm will not find any of those names because their positions do not match.

To address this, I recommend taking a different approach. You will need to create two for bonds within one another. The one outside goes through list 1 and the one inside goes through list 2, so as to compare all the elements of a list with all the elements of the other list. When you find an element that matches, you make cont++; and give break; . The break; is important, because once the list 1 element is found, you will not want to keep looking (and if you continue, you will have a problem with lists that have repeated elements).

Finally, one last detail:

return cont/lista2.length;

Here cont is integer and lista2.length is also integer. Therefore, an entire division will be carried out. Only after the integer division is made will the value be converted to a double to be returned. This is 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 percent, multiply by 100:

return ((double) (cont * 100)) / lista2.length;
    
10.07.2016 / 05:02
3

First of all, your algorithm does not define the size of the vectors at any time, before using them, you must define the size of each vector:

//neste exemplo iniciei os 2 vetores com o tamanho 4 indices
String [] lista1 = new String [4];
String [] lista2 = new String [4];

Next, you need to use a loop to fill in the vectors, respecting the size limit of each:

System.out.println ("Escreva os Nomes na Primeira Lista: ");

for(int i = 0; i < lista1.length; i++){
    lista1[i] = sc.nextLine();
}

System.out.println ("Escreva os Nomes na Segunda Lista: ");

  for(int i = 0; i < lista2.length; i++){
    lista2[i] = sc.nextLine();
}

According to the logic stated in the question, there is a problem with your calcularLista method, it is not counting or doing the operation correctly.

You need to go through the two lists to check which indices that list1 has in common with list2, and stop the execution of the second loop when an identical name is found in the two lists:

for (int i = 0; i<lista1.length;i++) {

    for(int z = 0; z<tamanhoLista2;z++){

        if (lista1[i].equals(lista2[z])) {

            cont++;
            break;
        }
    }
}

With this, the method will compare each index of list1 with each index of list2 and count only when they are found equal in both lists.

And to calculate the percentage, you need to divide the result of the found items multiplied by 100 by the size of list2, but before you need to transform one of the values to double, in the example, I made the size of list2 in double: p>

  public static double calcularLista(String lista1[], String lista2[]) {

        int cont = 0;
        double tamanhoLista2 = lista2.length;

        for (int i = 0; i<lista1.length;i++) {

            for(int z = 0; z<tamanhoLista2;z++){

                if (lista1[i].equals(lista2[z])) {

                    cont++;
                    break;
                }
            }
        }

        return (cont * 100)/tamanhoLista2;
    }

See your entire code working at ideone .

    
10.07.2016 / 04:54