# Building a Manipulate and an ArrayList with JLink

I'm wondering if it's possible to put this code inside a Manipulate. I tried just like this and got the results in the message box:

Then when I selected a diferent word from the PopMenu I got this:

Thought the problem was the ...Print[...] instructions so tried to build an arraylist like this but totaly failed, there's no result...

Needs["JLink"]

Scanner[archivo_String, palabra_String] :=
JavaBlock@
Module[{entrada, linea, numlinea, contiene, ruta, lista, renglon,
nop}, numlinea = 1; linea = ""; contiene = False;
InstallJava[];
ruta = JavaNew["java.io.File", archivo];
lista = JavaNew["java.util.ArrayList"];
(*Print["File: " <> ruta@getName[]];
Print["Word:" <> palabra];*)
If[JavaNew["java.lang.String", linea]@
contains[MakeJavaObject@palabra],
renglon =
JavaNew["java.lang.String",
"Line " <> ToString[numlinea ] <> ": " <> ToString[linea]];
contiene = True;
JavaObjectToExpression[lista]];
numlinea++];
If[! contiene,
nop = JavaNew["java.lang.String",
ToString[palabra] <> " no se ha encontrado"];
JavaObjectToExpression[lista]]]


Don't know exactly how to do it, I'd appreciate your help.

• Without having looked too closely at your Scanner, the question in the title is answered with something like list = JavaNew["java.util.ArrayList"]; list@add[MakeJavaObject@1]; list@add[MakeJavaObject@"bob"]; list@add[MakeJavaObject@{"alice"}]; JavaObjectToExpression@list Sep 7 '20 at 2:04
• Your Scanner function doesn't return anything if it finds ! contiene to be False - your last If doesn't have a third argument (the Else part). I also don't understand why you have a call to JavaObjectToExpression before the numlinea++ line when you aren't returning it at that point. Sep 7 '20 at 2:08

This has nothing to do with Manipulate or Print. Look at the result of Scanner[file, "issue"] and you will see that it is Null.

Look at your code, the Scanner function ends in a statement like this

If[! contiene, <do some stuff>]


You don't have a third argument for the If. And for any word contained in the file, contiene is True and so you are evaluating If[False, <code that is never reached]

Try this:

Scanner[archivo_String, palabra_String] := JavaBlock[
Module[
{entrada, linea, numlinea, contiene, ruta, lista, renglon, nop},
numlinea = 1;
linea = "";
contiene = False;
InstallJava[];
ruta = JavaNew["java.io.File", archivo];
lista = JavaNew @ "java.util.ArrayList";
linea = MakeJavaObject @ entrada @ nextLine[];
If[linea @ contains @ palabra,
renglon = MakeJavaObject @ StringJoin["Line ", ToString @ numlinea, ": ", JavaObjectToExpression @ linea];
contiene = True;

• Thank's. Tried and got a message about incorrect number or type in @contains ,  If[linea@contains@MakeJavaObject@palabra... fixed it. Sep 8 '20 at 2:34