Help Wanted - Trying to replace text but stuck at returning it

I tried using the Java String Replace() function to replace some text. But I can't return it because it is a function. I tried returning it anyway but it didn't compile. I tried looking at the Builtin Text blocks source, but it was in JS. I am having trouble returning the replaced text.

This is a test extension I am making to test some things out...

So my question how do I return it. I apologize if this is a really basic question...
I am still a novice in Java...
Thanks in advance.

Show your java code what you are trying to do.

@SimpleObject(description = "Replaces text")
    public void ReplaceText(String txt, String fr, String to){
        return txt.replace(fr, to).toString();
        // return added for testing, and to return the text
        // tostring added for debugging
    }

It does not return, probably because it is a function, so main goal is how to return it?

@SimpleFunction(description = "Replaces text")
    public String ReplaceText(String txt, String fr, String to){
        return txt.replace(fr, to).toString();
        // return added for testing, and to return the text
        // tostring added for debugging
    }

Ok and one more thing, how do I put the text in a label or variable or textbox?
That's why I tried using @SimpleObject.

@SimpleFunction(description = "Replaces text")
    public String ReplaceText(String txt, String fr, String to){
        return txt.replace(fr, to);
        // return added for testing, and to return the text
        // tostring added for debugging
    }

To return something from a function, use the return type of the object instead of "void". In your case it is String. And you don't have to use toString(). because replace() returns String.

1 Like

Understood. Thanks for the advice.

Also, now this error


Execution Log

Started Compiling Project TextReplacer
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/IFbYv/classes
[javac] Compiling 1 source file to /compiler/android/build/IFbYv/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/IFbYv/com/pb/tr/TextReplacer.java:36: error: cannot find symbol
[javac] @SimpleObject(description = "Replaces text")
[javac] ^
[javac] symbol: method description()
[javac] location: @interface SimpleObject
[javac] /compiler/android/src/IFbYv/com/pb/tr/TextReplacer.java:36: error: annotation type not applicable to this kind of declaration
[javac] @SimpleObject(description = "Replaces text")
[javac] ^
[javac] 2 errors
[javac] 1 warning

image

?

@SimpleFunction instead of @SimpleObject. I copied your code for editing purposes and did not correct it.

That would've been the case, if I could compile the extension :grin:
Now it won't compile, and I've posted the log

Why do I need to do that? I want to put the text in a label, textbox, or variable, like the replace block in the Blocks Editor.

If I use @SimpleFunction I can't put any text objects like labels textboxes and variables, etc...

I want a block like this

image

Image Courtesy of MIT

@SimpleFunction(description = "Replaces text")
    public String ReplaceText(String txt, String fr, String to){
        return txt.replace(fr, to);
        // return added for testing, and to return the text
        // tostring added for debugging
    }

The block will be purple, just like Tim showed.
There is no @SimpleObject annotation for functions. You can only use @SimpleFunction, @SimpleProperty or @SimpleEvent. @SimpleObject is something completely different.

1 Like

Thank you for the solution Patryk! It worked, and I actually learned something today.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.