2017-04-06 77 views
0

我試圖創建一個密碼生成器(隨機字符串發生器),最終將發送到數據庫,但是當我在活動中使用下面的代碼的應用程序出現錯誤「的Java終止Java串隨機函數發生器.lang.ArrayIndexOutOfBoundsException:長度= 67;指數= 85" 此代碼應使用一個循環,以產生從字符串的字符數組返回一個隨機值。創建從一個數組的Android

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

剛剛更新這一行:'INT字符= r.nextInt(GenPass.length );' –

回答

0

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; 
} 
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; 
} 

試試這個功能