I tried to make the old code you suggested me a little faster by doing this changes:


Scanner[archivo_String, palabra_String] :=
  Module[{entrada, linea, numlinea, contiene, ruta}, numlinea = 1; 
   linea = ""; contiene = False;
   ruta = JavaNew["java.io.File", archivo];
   entrada = JavaNew["java.util.Scanner", ruta];
   Print["Archivo: " <> ruta@getName[]];
   Print["Texto a buscar:" <> palabra];
   While[entrada@hasNext[], linea = entrada@nextLine[]; 
    If[JavaNew["java.lang.String", linea]@contains[palabra], 
     Print["Linea:" <> numlinea <> ":" <> linea];
     contiene = True]; numlinea++]; 
   If[! contiene, Print[ palabra <> " no se ha encontrado"]]]

But I get this and don't know why. It's not working cause "issue" appears several times.

enter image description here

Hope you can help me


1 Answer 1


The contains method requires a java.lang.String (or some other implementation of the CharSequence interface), not a Wolfram Language expression. Try using contains[MakeJavaObject@palabra] instead of contains[palabra]. There are more messages after that is fixed, I haven't looked at those.

  • $\begingroup$ Thank's. The messages are about StringJoin. ...Print["Linea " <> ToString[numlinea]<> ": " <> ToString[linea]]; fixed it. $\endgroup$ Sep 6, 2020 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.