Pick string from a textarea and populate an object

Asked

Viewed 329 times

1

I wonder if there is any API,plugin, something of the kind, and of course some idea how I can get a pre-formatted text, like this:

Cod Nome    Celuler     Telefone    Tel_trabalho
123 Bruno   986159946   27054578    23345689
124 Carlos  986159947   27054579    23345690
125 Rafael  986159948   27054580    23345691
126 Vinicio 986159949   27054581    23345692
127 Sandro  986159950   27054582    23345693
128 Everaldo    986159951   27054583    23345694
129 Vanessa 986159952   27054584    23345695
130 João    986159953   27054585    23345696

And populate an object by inserting each line as part of its attributes.

  • 1

    There is a pattern of separation between items?

  • @Diegof each column is separated by t(tab);

  • And lines for normal line breaks( n)?

  • @Diegof yes, are separated by n.

1 answer

1

I solve this by making my own library based on class annotations. It’s very simple to do!

The idea is declare the different layouts of TXT files as Java classes, and then use a generic parser that, from a given layout (a Java class), interprets a text file, resulting in an instance of that same class or a list of instances.

Something like that:

@Layout(separator = LayoutSeparator.TAB)
class LayoutPessoa {

    @LayoutField(position = 0)
    Integer codigo;
    @LayoutField(position = 1)
    String nome;
    @LayoutField(position = 2)
    String celular;
    @LayoutField(position = 3)
    String telefone;
    @LayoutField(position = 4)
    String telefoneTrabalho
}

class Main {

    public void static main(String[] args) {

        LayoutParser parser = new LayoutParser(LayoutPessoa.class);

        List<LayoutPessoa> pessoas = parser.parse(conteutoArquivoTxt);

        for(LayoutPessoa pessoa : pessoas) {
            System.out.println(pessoa.nome);
        }
    }
}

The annotation @Layout there is only illustrative, to demonstrate that the parser could read different separators besides tab (such as comma and semicolon), if you needed.

You may also have note specializations @Layoutfield to read numbers or formatted dates, and enter the format in specific properties of these other annotations.

In case, you’d have something like:

  • @Datelayoutfield(position = 5, format = dd/MM/yyyy)
  • @Numerlayoutfield(position = 6, format = #0.00).

In case of need, you can reuse almost all the code to make the reverse operation using the same layout statement, ie you can use the same layout statement both to read the from text file to objects how much to read from objects to text file.

In short, to implement this you will need only:

  • How to read lines from a text file;
  • Know how to declare and read annotations of a class;
  • Know how to instantiate classes via Reflection.

Once you have the lines of the files represented in data structures, it’s very easy to do whatever you want with this in Java, for example map to database via JPA, serialize to send over the network, etc.

Of course I’m considering that you have to read more than one layout, and eventually you’re always adding the readability of new layouts to the project. If you only have one type of text file to read, then it’s not worth it.

  • Thank you for your reply! I currently use this solution, but I need to delete the reading of a txt file or any other. I need the values to come straight from a textarea.

  • your library is available somewhere?

  • @Brunotinelli Ué, what’s the difference between parsing text from a file or from a Textarea component or any other source? My code is not available, the projects where I implemented this solution were all proprietary. However if you have specific questions just ask.

  • I use a similar solution, but I did not implement it. I am researching more about Reflection and Annotations to implement a similar solution, but without files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.