I'm new to Java, but I have a good amount of experience in JavaScript. I have a generated array with 15 random characters that I want to be converted to a String (ex. array { 0, 2, f, B, d, 7, A} being converted to 02fBd7A), but it creates this weird String of characters. It's like its actually taking the array and putting the syntax directly into the string (ex. array { 0, 2, f, B, d, 7, A}, instead of being "02fBd7A", being "[ 0, 2, f, B, d, 7, A ]" as a string. I'm much better versed in JavaScript, and I know you can manually trim the string in JavaScript, but is there an easier way to do this in Java? Code below (I'm aware that it can't generate capital letters like the examples above, but it's a simple thing to add I think).
import java.util.Arrays;
import java.util.Scanner;
public class passwordGen {
public static char Number(char[] array) {
byte random = (byte)(Math.random() * 10);
return array[random];
}
public static char Char(char[] array) {
byte random = (byte)(Math.random() * 26);
return array[random];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final char[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
final char[] alphabet = { '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'
};
float selector;
int length = scanner.nextInt();
char[] passHold = new char[length];
String passFinal;
try {
for (int i = 0; i < length; i++) {
selector = (float)Math.random();
if (selector < 0.5) {
passHold[i] = Number(numbers);
} else {
passHold[i] = Char(alphabet);
}
}
passFinal = Arrays.toString(passHold);
System.out.println(passFinal);
} catch (Exception e) {
System.out.println("An error occurred.");
}
}
}