【问题标题】:How can i get all words from a string array using a return statement如何使用 return 语句从字符串数组中获取所有单词
【发布时间】:2023-03-17 20:37:01
【问题描述】:

我想一个接一个地从String 数组中返回单词。

public String CurrentString(int move) {
    int currentString = 0;
    EditText ed = (EditText) findViewById(R.id.ed);
    String[] strings = ed.getText().toString().split(" ");
    int newString = currentString   move;   
    if (newString >= strings.length) {
        // if the new position is past the end of the array, go back to the beginning           
        newString = 0;  
    } 
    if (newString < 0) {
        // if the new position is before the beginning, loop to the end      
        newString = strings.length - 1;    
    }    
    currentString = newString;
    Toast.makeText(getApplicationContext(), strings[currentString],Toast.LENGTH_LONG).show();
    return strings[currentString];  
}

问题是我上面的代码没有返回所有文本。请帮忙。

【问题讨论】:

  • 真的很难理解你的问题,因为代码是别的东西。请修复它!您的代码中也有一些错误! currentString ;??
  • 考虑查看 StringTokenizer。
  • “不返回所有文本”...嗯,是的,你从数组中返回一个元素

标签: java android


【解决方案1】:

似乎你没有做足够的“功课”并且在数组方面遇到问题,
(这就是人们投票反对的原因[这不是一个适合没有投入所需“研究工作”的初学者的网站,该网站将被淹没])。
当前的趋势是投反对票和/或发表讽刺评论;O)
此外,您的代码包含无法编译的错误,因此您甚至都懒得测试它! ;O(
幸运的是,您不会获得负面声誉!
认真地请做一些研究(谷歌它!)
这是一些可能有帮助的代码。使用 split 将字符串处理成字符串数组:

        String string = "I want a string array of all these words";//input string
//                       ^   ^  ^    ^     ^    ^  ^   ^    ^
//                       0   1  2    3     4    5  6   7    8 //index
        String[] array_of_words;//output array of words
        array_of_words = CurrentString(string);//execute method
        Log.i("testing", array_of_words[8]);//this would be "words" in this example

        //later you might want to process commas and full stops etc...

            public String[] CurrentString(String string) 
            {
                String[] array = string.split(" ");   //use space to split string into words 
                //With the advent of Java 5, we can make our for loops a little cleaner and easier to read    
                for ( String sarray : array ) //loop through String array
                {
                     Log.i("CurrentString", sarray );//print the words
                }
                     return array ;//return String array
            }

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2020-12-29
    • 2015-07-26
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多