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];
entrada = JavaNew["java.util.Scanner", ruta];
lista = JavaNew["java.util.ArrayList"];
(*Print["File: " <> ruta@getName[]];
Print["Word:" <> palabra];*)
While[entrada@hasNext[], linea = entrada@nextLine[];
If[JavaNew["java.lang.String", linea]@
contains[MakeJavaObject@palabra],
renglon =
JavaNew["java.lang.String",
"Line " <> ToString[numlinea ] <> ": " <> ToString[linea]];
contiene = True;
lista@add[renglon];
JavaObjectToExpression[lista]];
numlinea++];
If[! contiene,
nop = JavaNew["java.lang.String",
ToString[palabra] <> " no se ha encontrado"];
lista@add[nop];
JavaObjectToExpression[lista]]]
Don't know exactly how to do it, I'd appreciate your help.
Scanner
, the question in the title is answered with something likelist = JavaNew["java.util.ArrayList"]; list@add[MakeJavaObject@1]; list@add[MakeJavaObject@"bob"]; list@add[MakeJavaObject@{"alice"}]; JavaObjectToExpression@list
$\endgroup$Scanner
function doesn't return anything if it finds! contiene
to beFalse
- your lastIf
doesn't have a third argument (theElse
part). I also don't understand why you have a call toJavaObjectToExpression
before thenumlinea++
line when you aren't returning it at that point. $\endgroup$