Good friends, you will see that I have to read a txt file with data "name", "phone" and then perform the binary search of a name in the.
I have already read the txt file and separated the data in the commas, however I do not understand how I can send the array with the separated values to the method that will perform the search.
This is what I have in my main class
using System;
using System.IO;
using System.Collections;
namespace busquedaAgenda
{
class Agenda
{
//Arreglo que tendrá los datos separados
public static ArrayList Registro = new ArrayList();
//Método para separar los datos del archivo
public void leer(String nomBusca)
{
string linea; //Cada linea de texto
String[] separar; //Arreglo que tendrá por separados los datos
Contacto cont = new Contacto(); //objeto que servirá para ir guardando los registros
StreamReader archivo = new StreamReader("Contactos.txt");
while ((linea = archivo.ReadLine()) != null)
{
//Separar mediante comas
separar = linea.Split(',');
//Se establece el orden y los datos que se guardaran
cont.Nombre = separar[0];
cont.Telefono = separar[1];
//Se guarda en el registro
Registro.Add(cont);
}
archivo.Close();
//Llamar método que realizará la busqueda binaria
Contacto.busquedaBinaria(Registro, nomBusca);
}
static void Main(string[] args)
{
Agenda nuevo = new Agenda();
String nombre;
Console.WriteLine("\tAgenda telefónica\n");
//solicuta nombre a buscar
Console.Write("\nContacto a buscar: ");
nombre = Console.ReadLine().ToUpper();
//Enviar el nombre al método que leerá los datos
nuevo.leer(nombre);
//Contacto.busquedaBinaria(Registro, nombre);
}
}
}
This is my other class that has the method to perform the binary search
using System.Collections;
using System;
namespace busquedaAgenda
{
class Contacto
{
//Propiedades autoimplementadas
public String Nombre { get; set; }
public String Telefono { get; set; }
public Contacto()
{
this.Nombre = Nombre;
this.Telefono = Telefono;
}
public static void busquedaBinaria(ArrayList datos, string nomBuscar)
{
int izq, der, centro;
izq = 0;
der = datos.Count - 1;
centro = (izq + der) / 2;
Console.WriteLine(datos[centro]);
while (nomBuscar != datos[centro] && izq < der)
{
if (nomBuscar > datos[centro])
{
izq = centro + 1;
}
if (nomBuscar < datos[centro])
{
der = centro - 1;
}
centro = (izq + der) / 2;
}
if (nomBuscar == datos[centro])
{
Console.WriteLine($"El número {nomBuscar} se encuentra en la posición {centro}");
}
else
{
Console.WriteLine($"El número {nomBuscar} no se encuentra");
}
}
}
}
If someone were kind enough to tell me how to send the data from the Registry or a better way to separate the data, I would appreciate it.