Because when I search by name does not find the name inside the ArrayList?

0

In a file we store the data of the clients of a company with the format name, location. The program will copy that data into an ArrayList so that the user can choose between viewing the customer data of a locality or searching between the names.

    public class Repetir60 {
static ArrayList<String> datos = new ArrayList();
 public static void main(String[] args) throws FileNotFoundException {

    File f = new File("datos.dat");
    Scanner fichero = new Scanner(f);


    String linea;
    while (fichero.hasNext()) {
        linea = fichero.nextLine();

        datos.add(linea);

    }

    Scanner teclado = new Scanner(System.in);
    System.out.println("1 para buscar por nombre, 2 para buscar por localidad");
    int numero = teclado.nextInt();
    if (numero == 1) {
        buscarnombre( );

    }
    if (numero == 2) {
        buscarlocalidad();

    }

}

public static void buscarnombre( ) {
    String nombre;
    System.out.println("Escribe el nombre que quieres buscar");
    Scanner teclado = new Scanner(System.in);
    nombre = teclado.nextLine();

    for (int i = 0; i < datos.size(); i++) {

        if (datos.get(i).equals(nombre)) {
            System.out.println(datos.get(i));

        }

    }
}

public static void buscarlocalidad( ) {

    String localidad;
    System.out.println("Escribe la localidad que quieres buscar");
    Scanner teclado = new Scanner(System.in);
    localidad = teclado.nextLine();

}

}

    
asked by Héctor Huertas 11.01.2018 в 13:56
source

2 answers

0

A good solution would be to create a client class that stores the name and location like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Repetir60 {

    static ArrayList<Cliente> datos = new ArrayList();

    public static void main(String[] args) throws FileNotFoundException {

        File f = new File("datos.dat");
        Scanner fichero = new Scanner(f);

        String linea;
        while (fichero.hasNext()) {
            linea = fichero.nextLine();
            String[] parts = linea.split(";");
            datos.add(new Cliente(parts[0], parts[1]));

        }

        Scanner teclado = new Scanner(System.in);
        System.out.println("1 para buscar por nombre, 2 para buscar por localidad");
        int numero = teclado.nextInt();
        if (numero == 1) {
            buscarnombre();

        }
        if (numero == 2) {
            buscarlocalidad();

        }

    }

    public static void buscarnombre() {
        String nombre;
        System.out.println("Escribe el nombre que quieres buscar");
        Scanner teclado = new Scanner(System.in);
        nombre = teclado.nextLine();

        for (int i = 0; i < datos.size(); i++) {

            if (datos.get(i).getNombre().equalsIgnoreCase(nombre)) {
                System.out.println(datos.get(i));

            }
        }
    }

    public static void buscarlocalidad() {

        String localidad;
        System.out.println("Escribe la localidad que quieres buscar");
        Scanner teclado = new Scanner(System.in);
        localidad = teclado.nextLine();

    }
}

class Cliente {

    String nombre;
    String localidad;

    public Cliente(String nombre, String localidad) {
        this.nombre = nombre;
        this.localidad = localidad;
    }

    public String getNombre() {
        return nombre;
    }

    public String getLocalidad() {
        return localidad;
    }
}
    
answered by 11.01.2018 / 20:35
source
0

Your code is looking for something that is wrong ..

You are not showing the input data, but in the search you are doing

datos.get(i).equals(nombre)

Suppose you entered the name Juan .. and in your file says:

Juan, America

Therefore, the if(datos.get(i).equals(nombre)) will always give false, because john is not equal to john; america. What's more, if you look for america you have the same problem.

The possible solutions you have are:

  • Separate the data string into two different arrays, and search in which correspond.
  • Create a class that contains both data and search where appropriate
  • Do a search but a piece of the string and not the complete one.
answered by 11.01.2018 в 18:23