我怎样才能从一个字符串数组使用返回语句的所有单词
问题描述:
我想要一个接一个地从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];
}
问题是我的上面的代码没有返回所有的文本。请帮忙。
答
似乎你没有做足够的“家庭作业”,并且在数组中有问题,
(这就是为什么人们投票的原因[这不是一个没有进行必要的“研究工作”的初学者的网站,现场将是淹没])。
目前的趋势是downvote和/或留下一个讽刺评论; 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
}
这是真的很难理解你的问题,因为代码是另一回事,请修复它也有在你的代码的一些bug ** currentString;!** ?? –
考虑看的StringTokenizer – JoxTraex
“没有按不会返回所有文本“...嗯,是的,你从数组中返回一个元素 –