How to Store Splitted Strings in an Arraylist?

Asked

Viewed 407 times

1

I’m doing a job where I need to save the information from a current account, I need to read the information from a file (text) and split this information into account code, name and balance. Also, I need to save this information to an Array.

My problem is this: I cannot split this information to save to an Array. Could you help me?

I created a reader function();

public void leitor() throws FileNotFoundException, IOException{ 
    try (
             BufferedReader reader = new BufferedReader (new FileReader ("arquivo.txt"));
        ) {
             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                 **String saida = line.split(";");**
                 System.out.println(saida);
             }
        }
}

Which just reads the file information, however, I can’t crack that information.

  • would like to add an Addendum on Arraylist: here. I believe it can help in understanding your question. Because of the array and arraylist tags

2 answers

1

See a simple way using the static method Arrays.asList(), but "splintando" spaces:

String str = "A HBO anunciou a sétima temporada de Game of Thrones";       
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(str.split(" ")));

To be separated by a point and a comma, but to place .split(";");

  • would like to add an Addendum on Arraylist: here. I believe I can help you understand your answer.

  • @pss1support did not understand exactly what you want! xD

  • No, no .... just use Arraylist and understand that they are objects of the List interface. and added the reference of my answer can help in understanding the use. Understand!?

  • @pss1support can use the will as a reference. This answer helps the Chief very well, but he asked and disappeared. It happens a lot around here. Too bad the answer is not validated. But I guarantee, it works. = D abs

  • I believe we have responded to the whole community, of course the author’s validation is very useful, but it is not the only purpose that motivates me to answer.

  • @pss1support is for community yes, but the author’s validation is more proof than mine, the code works. Remembering when there are other answers depending on the situation, there are several ways to solve a problem. There the evaluation of which answer is more viable, is not the one that is validated, but the one that has the highest reputation.

  • True! But even when it doesn’t work, it’s great also because we learn the same way and more deeply. Reputation represents privileges and is self-regulated and by meritocracy. I agree with you!

Show 2 more comments

-3

instead of doing String saida = line.split(";");

ago

var saida = line.split(';');

or else declare string[] saida

  • 2

    var? This does not exist in java.

Browser other questions tagged

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