从数组创建java字符串随机化器

从数组创建java字符串随机化器,java,android,arrays,Java,Android,Arrays,我正在尝试创建一个密码生成器(随机字符串生成器),它最终将发送到数据库,但当我在活动中使用以下代码时,应用程序终止时出现错误“java.lang.ArrayIndexOutOfBoundsException:length=67;index=85”这段代码应该使用循环生成字符串,从字符数组返回一个随机值 package com.example.zakratcliffe.androidantitheft; import android.content.Intent; import android.

我正在尝试创建一个密码生成器(随机字符串生成器),它最终将发送到数据库,但当我在活动中使用以下代码时,应用程序终止时出现错误“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;
}
}
package com.example.zakratcliffe.androidantife;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入java.util.Random;
公共类ProfileActivity扩展了AppCompatActivity{
私有TextView textViewUsername、textViewUserEmail;
私有编辑文本编辑描述,文本编辑密码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
如果(!SharedPrefManager.getInstance(this.isLoggedIn()){
完成();
startActivity(新意图(这个,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());
最终按钮按钮=(按钮)findViewById(R.id.generate_按钮);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串传递=生成的_传递();
textEditPassword.setText(pass);
}
});
}
生成的字符串\u pass(){
char[]GenPass={'a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','r','t','u','v','w','x','y','y','z','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','k','l','m','n','o','p','p','q','r','s','t','u','v','w','x','y','y','z','s','s','v','w','x','y','s';
int i=1;
字符串GeneratedPass=“”;
而(i<13){
随机r=新随机();
int字符=r.nextInt(66-0)+66;
System.out.println(GenPass[Character]);
GeneratedPass=GeneratedPass+GenPass[字符];
i++;
}
返回生成通行证;
}
公共void SendToDB(视图v){
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
病例R.id.menuLogout:
SharedPrefManager.getInstance(this.logout();
完成();
startActivity(新意图(这个,LoginActivity.class));
打破
}
返回true;
}
}
do

int字符=r.nextInt(66)

而不是

int字符=r.nextInt(66-0)+66

由于“r.nextInt(66-0)+66”可能会生成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;
}
while(i<13){
随机r=新随机();
int字符=r.nextInt(GenPass.length);
System.out.println(GenPass[Character]);
GeneratedPass=GeneratedPass+GenPass[字符];
i++;
}
返回生成通行证;
}
生成的字符串\u 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;
字符串GeneratedPass=“”;
int min=0;
int max=54;
而(i<13){
随机r=新随机();
int字符=r.nextInt(max-min+i)+min;
System.out.println(GenPass[Character]);
GeneratedPass=GeneratedPass+GenPass[字符];
i++;
}
返回生成通行证;
}

尝试此功能

只需更新此行:
int Character=r.nextInt(GenPass.length)
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;
}