我正在尝试创建一个最终发送到数据库的密码生成器(随机字符串生成器),但是当我在一个活动中使用下面的代码时,应用程序终止时出现错误“java.lang.ArrayIndexOutOfBoundsException:length = 67; index = 85“此代码应使用循环从字符数组中返回一个随机值以生成字符串。
package com.example.zakratcliffe.androidantitheft;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class ProfileActivity extends AppCompatActivity {
private TextView textViewUsername, textViewUserEmail;
private EditText textEditDescription, textEditPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, LoginActivity.class));
}
textViewUsername = (TextView) findViewById(R.id.textViewUsername);
textViewUserEmail = (TextView) findViewById(R.id.textViewUseremail);
textEditDescription = (EditText) findViewById(R.id.editTextDescription);
textEditPassword = (EditText) findViewById(R.id.editTextPassword);
textViewUserEmail.setText(SharedPrefManager.getInstance(this).getUserEmail());
textViewUsername.setText(SharedPrefManager.getInstance(this).getUsername());
final Button button = (Button) findViewById(R.id.generate_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pass = Generated_pass();
textEditPassword.setText(pass);
}
});
}
String Generated_pass(){
char[] GenPass = {'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', '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', '@', '!', '?', '&', '%', '~'};
int i = 1;
String GeneratedPass = "";
while (i < 13){
Random r = new Random();
int Character = r.nextInt(66 - 0) + 66;
System.out.println(GenPass[Character]);
GeneratedPass = GeneratedPass + GenPass[Character];
i++;
}
return GeneratedPass;
}
public void SendToDB(View v){
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menuLogout:
SharedPrefManager.getInstance(this).logout();
finish();
startActivity(new Intent(this, LoginActivity.class));
break;
}
return true;
}
}
答案 0 :(得分:0)
做
int Character = r.nextInt(66);
而不是
int Character = r.nextInt(66 - 0)+ 66;
as&#34; r.nextInt(66-0)+ 66&#34;可以生成0到66之间的任何随机数,然后添加到该数字66,因此该字符的值大于66,这在数组中是不可访问的(长度为66),因此异常arrayIndexOutOfBoundsException。
或者为了使代码更健壮,而不是硬编码66,请使用:
while (i < 13){
Random r = new Random();
int Character = r.nextInt(GenPass.length);
System.out.println(GenPass[Character]);
GeneratedPass = GeneratedPass + GenPass[Character];
i++;
}
return GeneratedPass;
}
答案 1 :(得分:0)
String Generated_pass(){
char[] GenPass = {'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', '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', '@', '!', '?', '&', '%', '~'};
int i = 1;
String GeneratedPass = "";
int min = 0;
int max = 54;
while (i < 13){
Random r = new Random();
int Character =r.nextInt(max - min + i) + min;
System.out.println(GenPass[Character]);
GeneratedPass = GeneratedPass + GenPass[Character];
i++;
}
return GeneratedPass;
}
试试此功能