2
$\begingroup$

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: enter image description here

Then when I selected a diferent word from the PopMenu I got this: enter image description here

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.

$\endgroup$
2
  • 2
    $\begingroup$ 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 $\endgroup$
    – Jason B.
    Sep 7, 2020 at 2:04
  • $\begingroup$ 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. $\endgroup$
    – Jason B.
    Sep 7, 2020 at 2:08

1 Answer 1

1
$\begingroup$

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];
        entrada = JavaNew["java.util.Scanner", ruta];
        lista = JavaNew @ "java.util.ArrayList";
        While[entrada @ hasNext[],
            linea = MakeJavaObject @ entrada @ nextLine[];
            If[linea @ contains @ palabra,
                renglon = MakeJavaObject @ StringJoin["Line ", ToString @ numlinea, ": ", JavaObjectToExpression @ linea];
                contiene = True;
                lista @ add @ renglon;
                JavaObjectToExpression @ lista
            ];
            ++numlinea
        ];
        If[!contiene,
            lista @ add @ MakeJavaObject @ StringJoin[palabra, " no se ha encontrado"]
        ];
        Column @ JavaObjectToExpression @ lista
    ]
]
$\endgroup$
1
  • $\begingroup$ Thank's. Tried and got a message about incorrect number or type in @contains , ` If[linea@contains@MakeJavaObject@palabra... fixed it. $\endgroup$ Sep 8, 2020 at 2:34

Your Answer

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

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