java.lang.NumberFormatException:无效的浮点数:"x"在Android中 [英] java.lang.NumberFormatException: Invalid float: "x" in android
问题描述
我正在尝试在字符串数组中获取随机值.我想在Textview中显示它们.我写了下面的代码.我正在获取NumberFormatException.
I am trying to get a random values in string array. I want to display them in Textview. I wrote the code below. I am getting NumberFormatException.
请帮助我!
如何解决我的问题,如何在textview中显示随机值?
How can I resolve my problem and how can I display random values in my textview?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int i1,i2,i3,i4,i5;
String [] strings;
TextView capta_Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
capta_Text = (TextView)findViewById(R.id.captaid);
strings = new String [] {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
List<String> stringList = new ArrayList<String>(Arrays.asList(strings));
loadingCaptha();
}
private void loadingCaptha(){
try{
String randomStr = strings[new Random().nextInt(strings.length)];
int arc4random = Integer.parseInt(randomStr);
i1 = (arc4random % strings.length)-1;
i2 = (arc4random % strings.length)-1;
i3 = (arc4random % strings.length)-1;
i4 = (arc4random % strings.length)-1;
i5 = (arc4random % strings.length)-1;
String captaText = strings[i1]+""+strings[i2]+""+strings[i3]+""+strings[i4]+""+strings[i5];
capta_Text.setText(captaText);
}catch (NumberFormatException e){
e.printStackTrace();
}
}
}
推荐答案
通过快速浏览您的代码,该错误对于我来说似乎很明显.查看您的 strings
数组定义:
The error seems fairly obvious to me from a quick glance at your code. Look at your strings
array definition:
strings = new String [] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c",
"d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
您在其中添加了字母,然后尝试选择一个将其解析为整数:
You included letters in there, and then you try to select one a parse it as an integer:
String randomStr = strings[new Random().nextInt(strings.length)];
int arc4random = Integer.parseInt(randomStr);
出于明显的原因,这将无法正常工作.如果您想从 strings
数组中随机选择5个字母来构建验证码,只需执行以下操作:
This won't work for obvious reasons. If you want to randomly select 5 letters from the strings
array to build your Captcha, then just do that:
Random rand = new Random();
i1 = rand.nextInt(strings.length);
i2 = rand.nextInt(strings.length);
i3 = rand.nextInt(strings.length);
i4 = rand.nextInt(strings.length);
i5 = rand.nextInt(strings.length);
String captaText = strings[i1]+""+strings[i2]+""+strings[i3]+""+strings[i4]+""+strings[i5];
capta_Text.setText(captaText);
这篇关于java.lang.NumberFormatException:无效的浮点数:"x"在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!