2016-10-10 8 views
-5

String配列の単語を1つずつ返したいと思います。returnステートメントを使用して文字列配列からすべての単語を取得するには

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]; 
} 

問題は、上記のコードがすべてのテキストを返さないということです。助けてください。

+0

コードが何か他のものであるため、あなたの質問を理解することは本当に難しいです、それを修正してくださいまた、いくつかのバグがあなたのコードである** currentString;。!!** ?? –

+0

はStringTokenizerは見て考えてみましょう – JoxTraex

+0

「のdoesn。すべてのテキストを返しません "...うーん、配列から1つの要素を返す –

答えて

0

あなたは十分な「宿題」を行っておらず、配列に問題があるようです。
これは人々がダウン投票している理由です。これは必須の「研究努力」をしていない初心者のためのサイトではなく、サイトは浸水しました])。
Oの)
あなたのコードにはコンパイルされないエラーが含まれているので、テストすることさえ気にしませんでした! !!; O(
ラッキーあなたのためにあなたが否定的な評判を得ることができない
真剣にいくつかの研究(それをGoogleに行ってください)
ここでは役立つかもしれないいくつかのコードである文字列配列にあなたの文字列を処理するために、スプリットを使用します。

 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 
      } 
関連する問題