Compare lines with txt Java Android

Asked

Viewed 519 times

2

I would like to create a method that compares each line of my text view with each line of a txt file

here I go through the txt file

for example, in my text view are displayed the words

blue green yellow green

in my txt I have

black blue green yellow

I would like you to issue an alert about the green that is not in the txt file

implemented this

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); // saida do textview
        BufferedReader entrada = new BufferedReader (new FileReader("bd.txt")); // arquivo txt
        int linha = 1;
        String line = "";
        String line1 = "";
       while ((line1 = entrada.readLine())!= null){
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
                if (line.contains(line1) == true) {

                // o que tem no textview tem no txt


                } else if (line.contains(line1) == false) {



                }

1 answer

0

You need to better specify how words will be written in your text and textview file. Whether they will all be on the same line or each one separated by a line break.

Anyway, I did a general implementation that works for both cases:

  BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); // saída do textview  
  BufferedReader entrada = new BufferedReader(new FileReader("bd.txt")); // arquivo txt

  String line = "";

  String coresTextView = ""; 
  String coresArquivoTexto = "";

  while((line = reader.readLine()) != null) 
    coresTextView = coresTextView + line + " ";

  while((line = entrada.readLine()) != null) 
    coresArquivoTexto = coresArquivoTexto + line + " ";

  String[] cores = coresTextView.split(" ");

  for(int i = 0; i < cores.length; i++) 
    if(coresArquivoTexto.replaceFirst(cores[i], "").equals(coresArquivoTexto)) 
      System.out.println("AVISO: " + cores[i] + " não está no arquivo texto!");
    else
      coresArquivoTexto = coresArquivoTexto.replaceFirst(cores[i], "");

  reader.close();
  entrada.close();

To solve this problem you need to think of two lists: one with the colors of the textview and the other with the colors of your text file.

You will take the colors of the textview one by one and compare it to the entire color list of your text file. If you find the color of your textview list in the color list of the text file, you can remove the color from the text file list to avoid running the risk of finding it again if you have the same color twice in the textview color list.

In my implementation, I used Strings as the "color lists" I cited in the explanation above. A String coresTextView takes lines from the textview and makes a space-separated join. Then, separates all words from the string by space and plays in the string array colors. With this, I have each color of the textview in a position of my String array colors.

A String colors is a string that joins all the colors found in the text file, separating them by space.

Soon after comes the loop to scroll through all the colors found in the textview. Inside the loop has the condition that makes the comparison to see if the color is present in the text file:

if(coresArquivoTexto.replaceFirst(cores[i], "").equals(coresArquivoTexto))

Here is a simple manipulation of String. If the color vector of the textview Color String is not present in the Color String of the text file, then the function .replaceFirst will return to String colors without any change. With this, we know that the color is not present in the text file.

Now, if the color vector of the textview color strings is present in the text file, let’s update the string colors removing the color we just checked. We do this by associating the variable to the function .replaceFirst that must return the String without the color passed as parameter.

coresArquivoTexto = coresArquivoTexto.replaceFirst(cores[i], "");

And it does the job you want it to!

I don’t know how many colors and text file sizes you intend to work with, it may be that maybe the String type variable used does not support as many words of the text file or textview you will use. In this case, try to change the type to lists, for example, or even associate the color names to 1-9 numbers and you can have a String with thousands of code characters that indicate colors.

Note: In this implementation take care of spaces in your text and textview files, remember that the function .split(" ") will split the String exactly into a space, if you have more than one space between the colors of your files, this can cause problems in running the program

  • in case, the txt file would be in the Assets directory??

  • In the main folder of the project.

Browser other questions tagged

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