I can not solve this problem c #

0

Hello, can someone tell me how to solve this?

Define a Person class with 3 fields: Name, Age and DNI. Write an algorithm that allow the user to enter in a console a series of data of the form "NameDocumentAge". Once the entry of data, the program should print in the console a listing the list with the form: No.) Name (Age) DNI.

Example:

  • Juan Perez (40) 2098745

  • José García (41) 1965412

  • a) Using a People arrangement. Before starting loading, the user must enter by console the number of people who will load.

    b) Using a arraylist. In this case the user should not enter the amount of people that will be loaded, simply the input process ends with a string empty.

    This is what I do:

    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace borrador
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("Ingrese la cantidad de personas que se van a ingresar:");
                int x = int.Parse(Console.ReadLine());
                string [] personas = new string[x];
                Console.WriteLine("Ingrese sus datos con el siguiente formato (Nombre Edad DNI): ");
                string datos = Console.ReadLine();
                int contador = 0;
                while (contador <x)
                {
                    personas[contador] = datos;
                    Console.WriteLine("siguiente Nombre Edad DNI:");
                    datos = Console.ReadLine();
                    contador += 1;
                }
    
                Persona programa = new Persona(personas);
                Console.ReadLine();
            }
        }
    
        class Persona
        {
            public string nombre, edad, dni;
            public Persona(string[] datos)
            {
                for (int i = 0; i < datos.Length; i++)
                {
                    string s=datos[i].Split(" ");
                    this.nombre=datos(0);
                    this.edad=datos(1);
                    this.dni=datos(2);
                    Console.WriteLine(nombre,edad,dni);
                }
    
            }
    
        }
    }
    
        
    asked by Matias Falcon 24.04.2018 в 23:43
    source

    2 answers

    1

    I would do this, it depends on whether you are obliged to use arrays, I would use List < > since it is more versatile,

    in the case of array

    int cantidad = int.Parse(Console.ReadLine());
    List<Persona> listaPersonas = new List<Persona>()
    
    for(int i = 0; i<cantidad; i++)
    {
      Console.WriteLine("Ingresar: Nombre Edad DNI:");
      var persona = Console.ReadLine().Split(" ").ToArray(); 
      //obtenés un array string[]
    
      //si sabés que siempre tienen 3 atributos las personas
      Persona p = new Persona();
      p.Edad = persona[0];
      p.Nombre = persona[1];
      P.Dni = persona[2];
    
      listaPersonas.Add(p);
    }
    
    
    public class Persona
    {
       public int Edad {get;set;}
       public string Nombre {get;set;}
       public string Dni {get;set;}
    
    }
    
        
    answered by 25.04.2018 в 00:22
    1

    ok, your code is full of errors .. I start

    how are you doing split by spaces "", the entry text " Juan Perez (40) 2098745 " will never work

    what you have to do is make two splits.

    first

     string s1=datos[i].Split("(");
    

    doing this s1 [0] will have the name, and s1 [1] will have the age and ID so we must divide s1 [1] to extract the age and the DNI

     string s2=s1[1].Split(")");
    

    With this:
    s1 [0] = Name
    s2 [0] = Age
    s2 [1] = DNI

    You probably need a trim to eliminate the remaining blank spaces.

    next error:

     *string datos = Console.ReadLine();*
     int contador = 0;
     while (contador <x)
     {
           personas[contador] = datos;
           Console.WriteLine("siguiente Nombre Edad DNI:");
           **datos = Console.ReadLine();**
           contador += 1;
    }
    

    Why are you reading the data twice? you have to learn to use cycles correctly, this is causing the program to ask for an additional line of input, but that data is never entered into the array of people.

     int contador = 0;
     while (contador <x)
     {
           datos = Console.ReadLine();
           personas[contador] = datos;
           contador++;
           if(contador <x)
           {
                Console.WriteLine("siguiente Nombre Edad DNI:");
           }
     } 
    

    now, the final problem: you are only creating a person object, what you have to do is create at the beginning of your void main an array of people as well:

     Persona[] PERSONAS = new Persona[x];
    

    and in each part of the cycle create an instance of each object:

     int contador = 0;
     while (contador <x)
     {
           datos = Console.ReadLine();
           personas[contador] = datos; //y podemos eliminar esto
           PERSONAS[contador] = new Persona(datos); 
           contador++;
           if(contador <x)
           {
                Console.WriteLine("siguiente Nombre Edad DNI:");
           }
     } 
    

    Full code:

    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace borrador
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("Ingrese la cantidad de personas que se van a ingresar:");
                int x = int.Parse(Console.ReadLine());
                string [] personas = new string[x];
                Persona [] PERSONAS = new Persona[x];
                Console.WriteLine("Ingrese sus datos con el siguiente formato (Nombre Edad DNI): ");
                string datos = Console.ReadLine();
                int contador = 0;
                while (contador <x)
                {
    
                    datos = Console.ReadLine();
                    personas[contador] = datos;
                    contador++;
                    if(contador <x)
                    {
                        Console.WriteLine("siguiente Nombre Edad DNI:");
                    }
                }
                for(int c=0;c<x;c++)
                {
                    PERSONA[c] = new Persona(personas[c]);
                }
                Console.ReadLine();
            }
        }
    
        class Persona
        {
            public string nombre, edad, dni;
            public Persona(string datos)
            {
                for (int i = 0; i < datos.Length; i++)
                {
                    string s1=datos.Split("(");
                    string s2=s1[1].Split(")");
                    nombre=s1[0];
                    edad=s2[0];
                    dni=s2[1];
                    Console.WriteLine(nombre,edad,dni);
                }
    
            }
    
        }
    }
    
        
    answered by 27.04.2018 в 19:16