Home > OS >  Java Arrays.toString() creates a strange sequence of characters
Java Arrays.toString() creates a strange sequence of characters

Time:10-07

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.");
        }
    }
}

CodePudding user response:

The Arrays.toString method creates a String that shows you each element in an array. What you're looking for is the String constructor that takes in an array of chars:

passFinal = new String(passHold);

CodePudding user response:

Or you could just concatenate newly selected chars onto the passFinal String and skip the intermediate char array. (You mentioned you wanted something like concatenating in one of your comments.)

String passFinal = "";
try {
    for (int i = 0; i < length; i  ) {
        selector = (float)Math.random();
        if (selector < 0.5) {
            passFinal  = Number(numbers);
        } else {
            passFinal  = Char(alphabet);
        }
    }

    System.out.println(passFinal);
}

Probably better is using StringBuilder and its append-

StringBuilder passFinalSb = new StringBuilder();
try {
    for (int i = 0; i < length; i  ) {
        selector = (float)Math.random();
        if (selector < 0.5) {
            passFinalSb.append(Number(numbers));
        } else {
            passFinalSb.append(Char(alphabet));
        }
    }
    String passFinal = passFinalSb.toString();
    System.out.println(passFinal);
}
  • Related