Request to make the code extensible

I have this code in JAVA:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TextFileSearch {

    public static void main(String[] args) {
        String filePath = "file.txt";
        String searchTerm = "world"; 

        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains(searchTerm)) {
                    System.out.println(line);
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And I want to make the code expandable, so that the file in the code will be loaded from a txt file in the application - according to the value entered for the file name in the application files, and also that the word to search for in the file will be a value received from the user in the extension.

What this code does: searches for a word in a text file, and if it finds the word, then it returns the word including the entire line where the link was found, for example if the file says:
what = yes
So if I search for “what” or “yes” then I get the whole above line.
It’s urgent for me, thanks.
Thanks!!

Can someone at least give an example of what I’m supposed to do?

It looks like you neither have Java experience nor experience in developing extensions…

But actually you already could search for some string in a file using existing blocks…

Read the file using the file component and search for your string using the contains block from the text drawer

Taifun

1 Like

Thanks, and how do I display the entire line where the string ends?
And in addition, how do I make a file that will contain words for the list? So I will load all the elements to the list from the file?

Read the file and convert it into a list using the split block, just split at \n to get each line as a list item
Then use a for each in list loop and check again using the contains block, it the current list item (i.e. the line) contains your string

Please elaborate

Taifun

1 Like

I have a txt file like this:
what, yes, hello,
And I want to use the file for the elements in the list element, how do I take this file and define that all the words in the file - that “,” separates word from word, will become items in the list?
And which extension is good for reading a txt file?

Use the file component to read the file

In the GotText event convert the text (which in your case is a csv row) using the list from csv row block into a list

See again Imagnity.com List Tutorial, Mirrored - @Saj - Tutorials and Guides - MIT App Inventor Community

Use the file component

Taifun

1 Like

Thanks!
In Read From, how do I specify reading from a txt file in the application files?

What about checking the documentation?

Read From¶

Reads text from a file in storage. Prefix the filename with / to read from a specific file on the SD card. for instance /myFile.txt will read the file /sdcard/myFile.txt. To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a filename does not start with a slash, it will be read from the applications private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.

Taifun

1 Like

I’ll just explain what I want to do…
I want to build an offline word translation application, for this purpose, I have a file of all the words in language 1, each word in a separate line, and another file of all the words in language 2, how can I create a translation based on this, what should I do?
I’m going crazy finding ideas…

Again something completely different

Well, don’t you think, this approach is dangerous? Only one error in the file and the translation is useless?
Have only file like this
apple,manzana
pineapple,piña
orange,naranja
…

Taifun

You could use the resource utilities component to create an offline tradution app, using json files containing the strings to be translated.
There are several ways to achieve the same result, as with Taifun above, following the guides and links and testing in practice to learn faster.

All in One Combine Both Lists Convert to json Array and Use the Standard Procedure to convert Json Array to list & Enjoy