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();
}
}