将txt文件中的每个单词分开并获取它。

huangapple go评论48阅读模式
英文:

Divide each word in txt file and get it

问题

我需要帮助,我正在开发一个应用程序,该应用程序将从资产中的txt文件中读取单词并将其显示在textView中,问题是它显示了文件中的所有文本,但我想单独获取每个单词。

例如:在资产中有一个名为words.txt的文件,其中包含单词“Android,Kotlin,Java”,我需要将仅“Kotlin”单词显示到文本中,但我不知道如何将其单独获取,而不是获取所有单词。

并且我想随机显示分隔的文本。

这是我的代码:

private fun randomWord() {
    val myOutput: String
    val myInputStream: InputStream

    try {
        myInputStream = assets.open("words.txt")
        val size: Int = myInputStream.available()
        val buffer = ByteArray(size)
        myInputStream.read(buffer)
        myOutput = String(buffer)

        val list = myOutput.split(",") // 这是我想要做的
        binding.textView.text = list.random() // 这是我想要做的

        //binding.textView.text = myOutput // 这是我可以使用的,但我不想要它

    } catch (e: IOException) {
        e.printStackTrace()
    }
}

希望这可以帮助你按照你的要求单独获取并随机显示单词。

英文:

I need help, I'm developing app that will read words from txt file in assets and display it to textView, problem is that it displays me all text that is in txt file, but i want to take each word separately.
For example: There is words.txt file in assets and there are words "Android, Kotlin, Java" and i need to display only "Kotlin" word to text, and I don't know how to get it separately, instead of it i get all words.
And i want to randomly display separated text.

There is my code:

private fun randomWord() {
        val myOutput: String
        val myInputStream: InputStream

        try {
            myInputStream = assets.open("words.txt")
            val size: Int = myInputStream.available()
            val buffer = ByteArray(size)
            myInputStream.read(buffer)
            myOutput = String(buffer)

            val list = myOutput.toList<String>() //its what i want to do
            binding.textView.text = list.random() //its what i want to do

            //binding.textView.text = myOutput //its that i can use but i dont want it

        } catch (e: IOException) {
            e.printStackTrace()
        }

答案1

得分: 1

问题在于您正在取整个文件,而没有将其拆分成一个列表。

val list = myOutput.toList<String>() //这是我想要做的

而不是这样做

val list = myOutput.split(",")

这将创建一个包含文件中逗号分隔字符串的列表

希望这有所帮助

英文:

The problem is that you are taking the whole file and not splitting it in a List

val list = myOutput.toList&lt;String&gt;() //its what i want to do

Instead of this, do this

val list = myOutput.split(&quot;,&quot;)

this will create a list containing the comma separated strings in your file

Hope this helps

答案2

得分: 0

以下是您的代码翻译:

private fun randomWord() {
    val myOutput: String
    val myInputStream: InputStream

    try {
        myInputStream = assets.open("words.txt")
        val size: Int = myInputStream.available()
        val buffer = ByteArray(size)
        myInputStream.read(buffer)
        myOutput = String(buffer)

        // 将字符串拆分为单词数组。
        val words = myOutput.split(" ")

        // 从数组中获取一个随机单词。
        val randomWord = words.random()

        // 在文本视图中显示随机单词。
        binding.textView.text = randomWord
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

希望这对您有所帮助!

英文:

Here is the code I have done, you can view it:

private fun randomWord() {
    val myOutput: String
    val myInputStream: InputStream

    try {
        myInputStream = assets.open(&quot;words.txt&quot;)
        val size: Int = myInputStream.available()
        val buffer = ByteArray(size)
        myInputStream.read(buffer)
        myOutput = String(buffer)

        // Split the string into an array of words.
        val words = myOutput.split(&quot; &quot;)

        // Get a random word from the array.
        val randomWord = words.random()

        // Display the random word in the text view.
        binding.textView.text = randomWord
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

huangapple
  • 本文由 发表于 2023年7月13日 20:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定