0

okay, so here it is the full code

public static ArrayList<String> procuraGeneroLiterario(String nome_escritor) throws IOException{
    String link = "https://pt.wikipedia.org/wiki/" + nome_escritor;
    String pesquisa =""; 
    ArrayList<String> GeneroLiterario = new ArrayList();
    HttpRequestFunctions.httpRequest(link,pesquisa,"ESCRITORES.txt");
    String GenLit_er0 = "<td scope=\"row\" style=\"vertical-align: top; text-align: left; font-weight:bold; padding:4px 4px 4px 0\">Gênero(s)</td>";
    String GenLit_er = "<a href=\"/wiki/.+ title=\".+\">(.+)</a>";

    String Genero = null;
    Scanner ler = new Scanner(new FileInputStream("ESCRITORES.txt"));
    Pattern p0 = Pattern.compile(GenLit_er0);
    Pattern p = Pattern.compile(GenLit_er);


    while (ler.hasNextLine()) {
        String linha = ler.nextLine();            
        Matcher f = p0.matcher(linha);
        if(f.find()){
            //linha = ler.nextLine();
            while(ler.hasNextLine()){
                linha = ler.nextLine();                  
                Matcher m = p.matcher(linha);

                if(m.find()){

                    Genero = m.group(1);
                    if (Genero != null){
                        for(int i = 0; i < Genero.size(); i++){
                            GeneroLiterario.add(Genero);
                    break;
                        }


                    }



                }


            }


        }
    }
    ler.close();
    return GeneroLiterario;


}

my teacher now told me to do but only if GeneroLiterario is declared as an ArrayList.

System.out.println("LISTA DE GeneroLiterarios: " );
   for (int i=0; i<this.GeneroLiterarios.size(); i++)
         System.out.println(this.GeneroLiterarios.get(i));

I made a class and there the GeneroLiterario is a String. Do i have to change it to arraylist? cause the output will be an arraylist of strings. and right now the output is [].

  • 3
    `java.lang.String` doesn't have method `size()`. Do you mean `length()`? – MikeCAT May 01 '16 at 10:48
  • I don't get what you're trying to do exactly. You're now iterating through the characters of Genero (if you change `size()` to `length()` as @MikeCAT suggests), and for each character you add the full string to `GeneroLiterario`? – Rick May 01 '16 at 10:58
  • Its easy to understand if you post complete code here.. – Geeth Lochana May 01 '16 at 11:50

1 Answers1

0

I'm guessing that you mean RE (regex) rather than ER, and that m is actually a Matcher instance.

In that case, you have misunderstood the Matcher.group(int) method. That method returns a String not a List<String>. That is why you are getting a compilation error saying that String does not have a size() method.

If you are trying to add all of the matched groups to GeneroLiterario, then you probably want this:

if (m.find()) {             
    for (int i = 1; i <= m.groupCount(); i++) {
        if (m.group(i) != null) {
            GeneroLiterario.add(m.group(i));
        }
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216