Save file data in ArrayList and display it from largest to smallest.

1

I am working on a program that obtains data from a text file, obtains only the first 100 and has a method that orders from the first to the last or vice versa and another that searches for a specific name in the file.
 This is what I have at the moment.

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

public class distritos
{

    public static void main(String[] args) 
    {
        Scanner entrada;
        File archivo;
        String a;
        String linea;
        ArrayList<Double> array = new ArrayList<Double>();
        try
        {
            entrada = new Scanner("Texto.txt");
            a = entrada.nextLine();
            archivo = new File(a);
            entrada = new Scanner(archivo);

            int comienzo = 0;
            while(entrada.hasNextLine() && comienzo < 100)
            {

                linea = entrada.nextLine();
                System.out.println(linea);
                comienzo++;
            }
        }
        catch(Exception e)
        {

        }
    }
}  

I try it and it works well, it gets the first 100 lines and prints them, however, I have problems when it comes to ordering it and I do not know where to start.

    
asked by Eleber 04.05.2018 в 22:06
source

1 answer

-1

Before printing the lines, you have to order them, for this you can use a ArrayList and enter the values of each line that contains the file

    ...
    ...
    ArrayList<String> lista = new ArrayList<String>();

    int comienzo = 0;
     while(entrada.hasNextLine() && comienzo < 100)
     {

         linea = entrada.nextLine();
        // System.out.println(linea);
         lista.add(linea); //Agrega valor a ArrayList
         comienzo++;
     }
     ...
     ...

Later you can use to alphabetize the arraylist Collections.sort () and then print the sorted values, like this:

        Collections.sort(lista);
        //Ahora imprime valores ordenados
        for(String valor: lista){
            System.out.println(valor);
        }

According to the code of your question, this would be the way to do it:

    Scanner entrada;
    File archivo;
    String a;
    String linea;
   // ArrayList<Double> array = new ArrayList<Double>();
    ArrayList<String> lista = new ArrayList<String>();
    try
    {
        entrada = new Scanner("Texto.txt");
        a = entrada.nextLine();
        archivo = new File(a);
        entrada = new Scanner(archivo);

        int comienzo = 0;
        while(entrada.hasNextLine() && comienzo < 100)
        {

            linea = entrada.nextLine();
           // System.out.println(linea);
            lista.add(linea); //Agrega valor a ArrayList
            comienzo++;
        }

        //Despues de agregar a ArrayList, ordena.
        Collections.sort(lista);
        //Ahora imprime los valores ordenados.
        for(String valor: lista){
            System.out.println(valor);
        }

    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    
answered by 05.05.2018 в 01:47