Skip to content
Advertisement

Convert 2 chars to a string

I have 2 chars I need converting to a string. I can convert one no problem but how do I turn 2 randomly generated chars into one string? Here is my code so far, the aim of my program is to generate two chars at random and return them:

JavaScript

Advertisement

Answer

A String concatenated with a char is a String1. So you could do,

JavaScript

or something like

JavaScript

Also, I’d really prefer to use a StringBuilder. I’d also make the length an argument, move the Random and String out of the method.

JavaScript

Then you could call it like pg.uppercaseLetters(2) or pg.uppercaseLetters(4) and get n letters (instead of 2).

1char is an integral value in Java, so char+char is an int.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement