JAVA. Doubt about looking for a String in a two-dimensional Array

0

I am looking for the operator to enter two related data, then two more and so on continuously. When you need to search for a data, you can enter any of the two related data and the other data will be displayed. Ex: First and last name, if I write name, it gives me the last name.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);


    System.out.print("Ingrese el número de paises que va a ingresar: ");
    int k = sc.nextInt();

    String[][] dato = new String[2][k];

    for (int i = 0; i < k; i++){ 
        for (int j = 0; i < 2; j++){ 
            if (j==0){    
            System.out.print("Ingrese el país: "); 
            dato[0][i] = sc.nextLine();
            }
            if (j==1){    
            System.out.print("Ingrese la capital: "); 
            dato[1][i] = sc.nextLine();
            }
        }  
        String search = null;


        while (!"FIN".equals(search)){
        System.out.println("Ingrese el dato a buscar: ");
        search = sc.nextLine();    

        String resultado = Arrays.stream(dato)
                         .filter(s -> s.equals(dato))
                         .findFirst()
                         .orElse(null);
        if (resultado != null) {
        System.out.println("");
        } else {
        System.out.println("No Encontrado");
        }
        }  
    }
}

From the "while" I no longer know how to achieve the expected result.

Beforehand, I appreciate your help. Greetings.

    
asked by S4nt0 14.09.2018 в 02:52
source

2 answers

0

You can try the following program:

public class PairsTest {

    private static String[][] countries;

    public static void main(String[] args) {

        PairsTest.fillData();
        PairsTest.searchData();
    }

    private static void fillData() {
        Scanner sc = new Scanner(System.in);

        System.out.print("Ingrese el número de países que va a ingresar: ");
        int num_paises = sc.nextInt();

        PairsTest.countries = new String[num_paises][];
        for (int i = 0; i < num_paises; i++) {
            String[] pair = new String[2];
            String readed = "";

            System.out.print("Ingrese el país: ");
            while (readed.equals("")) {
                readed = sc.nextLine();    
                pair[0] = readed;
            }
            System.out.print("Ingrese la capital: ");
            readed = sc.nextLine();
            pair[1] = readed;

            PairsTest.countries[i] = pair;
            System.out.println();
        }
    }

    private static void searchData() {
        Scanner sc = new Scanner(System.in);
        System.out.println("\n-----------------------------------------------");

        while (true) {
            System.out.println("Ingrese dato a buscar: ");
            String readed = sc.nextLine();    

            if ("FIN".equals(readed)) {
                break;
            }

            String result = "";
            for (String[] pair : PairsTest.countries) {
                if (pair[0].equals(readed)) {
                    result = pair[1];
                    break;
                }
                if (pair[1].equals(readed)) {
                    result = pair[0];
                    break;
                }
            }

            System.out.print("Resultado de buscar '" + readed + "': ");
            if (result.equals("")) {
                System.out.println("No Encontrado.");
            } else {
                System.out.println(result);
            }
            System.out.println();
        }  
    }

} // class

A possible result with two countries would be:

Ingrese el número de países que va a ingresar: 2
Ingrese el país: France
Ingrese la capital: Paris

Ingrese el país: Spain
Ingrese la capital: Madrid


-----------------------------------------------
Ingrese dato a buscar: 
Madrid
Resultado de buscar 'Madrid': Spain

Ingrese dato a buscar: 
xxx
Resultado de buscar 'xxx': No Encontrado.

Ingrese dato a buscar: 
Paris
Resultado de buscar 'Paris': France

Ingrese dato a buscar: 
FIN
    
answered by 14.09.2018 / 09:12
source
0

The problem when using filter is that you can not obtain the related data, I believe that the healthiest alternative and without resorting to external libraries would be like this:

String[] paises = dato[0]; //esto lo hago solo para que sea mas comodo leer el codigo
String[] capitales = dato[1];

String resultado = "";
for (int i=0;i<paises.length;i++) {
   if (paises[i].contains(search)) { //o equalIgnoreCase() si la busqueda tiene que ser exacta
      resultado = capitales[i];
      break;
   }
}
    
answered by 14.09.2018 в 07:25