초급의 끄적거림

[JAVA] 임시 비밀번호 생성 소스 본문

JAVA

[JAVA] 임시 비밀번호 생성 소스

codingD 2019. 9. 25. 17:13

다른 문자를 추가하고 싶을 경우, 배열에 ' ' (작은 따옴표) 안에 문자를 넣어서 추가하기

public class RandomPassword { 

public static String setPassword(int length) { 
int index = 0; 
char[] charArr = new char[] { '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' }; 

StringBuffer sb = new StringBuffer(); 

for (int i = 0; i < length; i++) { 
index = (int) (charArr.length * Math.random()); 
sb.append(charArr[index]); 
} 

return sb.toString(); 
} 

public static void main(String[] args) { 
	//파라미터 int값이 자리수 
	String password = setPassword(6); 
	System.out.println(password); 
}

}//class() end

 

Comments