如何使用return语句从字符串数组中获取所有单词

时间:2016-10-10 17:17:54

标签: java android

我想一个接一个地从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];  
}

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

1 个答案:

答案 0 :(得分:0)

似乎你没有做足够的“家庭作业”并且在阵列方面遇到问题,这(这就是为什么人们投票失败的原因[这不是一个没有投入所需“研究工作”的初学者的网站] ,该网站将被淹没]) 目前的趋势是downvote AND / OR留下讽刺评论; O)
此外,您的代码包含无法编译的错误,因此您甚至没有费心去测试它! O(
幸运的是,你不能得到负面的声誉!
说真的,请做一些研究(google it!)
以下是一些可能有用的代码。使用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
            }