1
I’m putting together a very simplistic project in which I show the processes that are running on android and compare it to a list in a txt file that has other processes. The intention is to check if all the processes that are appearing in textView, are in txt. If something appears in textView and this is not in TXT, be sent an alert showing what is in textView and not in txt. I thought of a solution using arraylist
1- in textview, the output is like this
com.whatsapp
com.snaptube.premium
com.youtube
com.facebook
separated by a line break
my text file is the same, only with more processes. Example
com.whatsapp
com.snaptube.premium
com.youtube
com.facebook
com.outroApp
the intention is, if texview appears some application that is not in txt the user receives a warning showing that that application is not in txt. it would be as if each line of the textview were compared with all lines of the text file. That’s why I thought of 2 for’s
what I have now:
ArrayList<String> texto = new ArrayList<String>(); // array para salvar o que esta no arquivo texto
ArrayList<String> textView= new ArrayList<String>(); // array para salvar o que mostra no textview
while ( (recebe_string = bufferedReader.readLine()) != null ) {
texto.add(recebe_string); // adiciono o que tenho no txt em um array
while ((line = reader.readLine()) != null) { // exibindo no textview
textView.add(parteFinal); //adiciono os processos que estao no textview em um array
now comes the part I can’t. Compare what you have in textview with txt
thought about it
for (int i = 0; i < texto.size() ;i++) { // percorro array do txt
for (int ii = 0; ii <textView.size() ; ii++) { // percorro array do textview
if (textView.get(ii).equals(texto.get(i))) // se tudo que tem no textview tem no txt nada a fazer
teste = false;
else{ // se tem algo no textView que nao tem no txt, avisa
teste = true;
virus = textView.get(ii); // capturo o que tem no textview e nao tem no txt
System.out.println("virus"+ textView.get(ii)); // exibo o que capturei
}
}
}
I’m having trouble comparing this part. this method I did it warns even if the process displayed in textView is in txt
Just a suggestion, wouldn’t it be simpler to use a
Set
to avoid iterating always (would only have a hehe)?– Wakim
No problem :D
– Wakim
@Wakim You’re right, it didn’t occur to me to use a Set. I usually start from the AP code to build my answer. I think your suggestion would be:
HashSet<String> virus = new HashSet<>(texto);
virus.removeAll(textView);
– ramaral
Thanks for the answers. However it is displaying the total content of TXT
– Gustavo Rotondo
Then it is because the strings are not equal in the two Arraylist or have spaces or special characters. The code I posted is correct (one was missing
{
see the edit). Try to dotrim()
in strings when comparing:textView.get(ii).trim().equals(texto.get(i).trim())
– ramaral
if the problem is special characters, I could use the contains()?
– Gustavo Rotondo
Not because you do not know who contains what. Anyway it would not be the best solution. The solution is to get the strings correctly.
– ramaral
When analyzing the output, I saw that it shows everything you see in TXT except what is in the text view. what I wanted is the opposite of would be because of that line? virusList.add(text.get(i));
– Gustavo Rotondo
I’ve been reading the comments in your code and in fact what you want is the opposite. I’ll edit the answer.
– ramaral
I expressed myself badly at that stage. then would just replace the for counters and in the add part in the arraylist virus, pick up what’s in the text view? I tried it and it didn’t work
– Gustavo Rotondo
apparently it is working now thank you very much
– Gustavo Rotondo