How to query within a Java array?

Asked

Viewed 121 times

0

The problem calls for the creation of a program that reads the name, Cpf, email and phone of 10 people. Then provide 4 query options (1-By name, 2-By CPF, 3-By email and 4-By phone). For example, if the user chooses option 2, the query should request a CPF and, based on it, should print the customer data that has this CPF. The program should inform if it does not find any client with the informed data. Use a 10 x 4 matrix to represent the data.

The problem is that it compiles right, asks for the information, informs the menu, but when I enter for example a name to query, nothing appears. What’s the matter?

import java.util.Scanner;

public class Exercicio10 {

    public static void main(String[] args) {

        Scanner ler = new Scanner(System.in); //leitura de dados da matriz
        Scanner lm = new Scanner(System.in); //leitura de dados do menu
        Scanner n = new Scanner(System.in); //leitura de nome
        Scanner c = new Scanner(System.in); //leitura de cpf
        Scanner e = new Scanner(System.in); //leitura de email
        Scanner t = new Scanner(System.in); //leitura de telefone


        String m[][] = new String [10][4];
        int i, j;

        for (i = 0; i < 10; i++) {
            System.out.println("Informe o nome, CPF, e-mail e telefone da " + (i+1) + "ª pessoa:");
            for (j = 0; j < 4; j++) {
                m[i][j] = ler.nextLine();
            }
        }


            System.out.println("Opções de consulta:");
            System.out.println("|1| - Consultar por nome");
            System.out.println("|2| - Consultar por CPF");
            System.out.println("|3| - Consultar por e-mail");
            System.out.println("|4| - Consultar por telefone");
            int menu = lm.nextInt();


            switch (menu) {
            case 1:
                System.out.println("Insira o nome:");
                String nome = n.nextLine();
                for (i = 0; i < 0; i++) {
                    if (nome == m[i][1]) {
                        System.out.println(m[i][1]);
                    }
                }
            break;
            case 2:
                System.out.println("Insira o CPF:");
                String cpf = c.next();
                for (i = 0; i < 0; i++) {
                    if (cpf == m[i][2]) {
                        System.out.println(m[i][2]);

                    }
                }
            break;
            case 3:
                System.out.println("Insira o e-mail:");
                String email = e.next();
                for (i = 0; i < 0; i++) {
                    if (email == m[i][3]) {
                        System.out.println(m[i][3]);
                    }
                }
            break;
            case 4:
                System.out.println("Insira o telefone:");
                String telefone = t.next();
                for (i = 0; i < 0; i++) {
                    if (telefone == m[i][4]) {
                        System.out.println(m[i][4]);
                    }
                }
            break;

            }

        }

}

1 answer

1


1) Comparison between Strings;

The main problem is that you are making the comparison with ==. The comparison between Strings should be made with .equals which makes the comparison by value and not by reference. Your comparisons within the if should look like this: m[i][1].equals(nome)

2) Lasso FOR

Your for loop has no stopping criteria virtually: for (i = 0; i < 0; i++). The way it’s written, it would only stop when I’m smaller than 0, but i already starts at 0, so it will add a drive to infinity or pop some exception. Be careful with this.

The correct one following your logic would be: for (i = 0; i < 10; i++).

3) Index of the Matrix

In Java, structures start at 0. That is, if you have a 2x2 matrix the positions you can access are: 0x0, 0x1, 1x0 and 1x1. So that you do not receive an error code, put the correct positions when accessing the positions of your matrix;

With these points, we will encompass all this in your code. I put as it was before commented and as it should be so that you can compare:

import java.util.Scanner;

public class Exercicio10 {

    public static void main(String[] args) {

        Scanner ler = new Scanner(System.in); //leitura de dados da matriz
        Scanner lm = new Scanner(System.in); //leitura de dados do menu
        Scanner n = new Scanner(System.in); //leitura de nome
        Scanner c = new Scanner(System.in); //leitura de cpf
        Scanner e = new Scanner(System.in); //leitura de email
        Scanner t = new Scanner(System.in); //leitura de telefone


        String m[][] = new String[10][4];
        int i, j;

        for (i = 0; i < 10; i++) {
            System.out.println("Informe o nome, CPF, e-mail e telefone da " + (i + 1) + "ª pessoa:");
            for (j = 0; j < 4; j++) {
                m[i][j] = ler.nextLine();
            }
        }


        System.out.println("Opções de consulta:");
        System.out.println("|1| - Consultar por nome");
        System.out.println("|2| - Consultar por CPF");
        System.out.println("|3| - Consultar por e-mail");
        System.out.println("|4| - Consultar por telefone");
        int menu = lm.nextInt();


        switch (menu) {
            case 1:
                System.out.println("Insira o nome:");
                String nome = n.nextLine();
//                for (i = 0; i < 0; i++) {
                for (i = 0; i < 10; i++) {
//                    if (nome == m[i][1]) {
                    if (m[i][0].equals(nome)) {
//                        System.out.println(m[i][1]);
                        System.out.println(m[i][0]);
                    }
                }
                break;
            case 2:
                System.out.println("Insira o CPF:");
                String cpf = c.next();
//                for (i = 0; i < 0; i++) {
                for (i = 0; i < 10; i++) {
//                    if (cpf == m[i][2]) {
                    if (m[i][1].equals(cpf)) {
//                        System.out.println(m[i][2]);
                        System.out.println(m[i][1]);

                    }
                }
                break;
            case 3:
                System.out.println("Insira o e-mail:");
                String email = e.next();
//                for (i = 0; i < 0; i++) {
                for (i = 0; i < 10; i++) {
//                    if (email == m[i][3]) {
                    if (m[i][2].equals(email)) {
//                        System.out.println(m[i][3]);
                        System.out.println(m[i][2]);
                    }
                }
                break;
            case 4:
                System.out.println("Insira o telefone:");
                String telefone = t.next();
//                for (i = 0; i < 0; i++) {
                for (i = 0; i < 10; i++) {
//                    if (telefone == m[i][4]) {
                    if (m[i][3].equals(telefone)) {
//                        System.out.println(m[i][4]);
                        System.out.println(m[i][3]);
                    }
                }
                break;

        }

    }

}

About the part "The program must inform if you do not find any customer with the informed data." has nothing implemented, but my tip is to use a logic with flag.

I hope I’ve helped.

  • thank you so much!!

Browser other questions tagged

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