[英]I just started using android and am having trouble with a reverseWord method
I made a method reverseWord
that is supposed to reverse an inputted word and display it. 我做了一个方法reverseWord
,它应该反转输入的单词并显示它。
public void reverseEncrypt(View v)
{
// Get the input
EditText input = (EditText) findViewById(R.id.editText);
TextView output = (TextView) findViewById(R.id.tv_result);
TextView output2 = (TextView) findViewById(R.id.tv_test);
output2.setText("Test is complete");
// Reverse word
String text = input.getText().toString();
String word = " ";
for (int i = text.length(); i > 0; i--)
{
word += text.substring(i,i-1);
}
output2.setText(text);
// Output result
output.setText("Result: " + word);
}
The method works fine until I output the string word
or text
. 该方法可以正常工作,直到我输出字符串word
或text
为止。 Other test outputs work, but when I try to output word
or text
it crashes. 其他测试输出也可以,但是当我尝试输出word
或text
会崩溃。 I think it isn't receiving the input correctly, but I am really not sure. 我认为它没有正确接收输入,但是我不确定。
This is probably a relatively easy fix, so any help would be appreciated. 这可能是一个相对容易的修复,因此将不胜感激。 If you need anymore information to help me, please do not hesitate. 如果您需要其他信息来帮助我,请不要犹豫。
I did not test it all, but your way of reversing the String did not work for me. 我没有全部测试,但是您反转String的方式对我不起作用。
For me this reversed the String and printed it. 对我来说,这将字符串反转并打印出来。
String text = "hello";
String reverse = new StringBuffer(text).reverse().toString();
System.out.println(reverse);
if you put that in your code, it should work 如果您将其放在代码中,它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.