0

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:

import java.util.Random;
public class PasswordGenerator {

    Random rand = new Random();

    public String uppercaseLetters() {
    char char1;
    char char2;
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String result;


    char1 = alphabet.charAt(rand.nextInt(25));
    char2 = alphabet.charAt(rand.nextInt(25));


    result = String.valueOf(char1, char2);
    return result;
    }

    public static void main(String[] args) {

    PasswordGenerator pg = new PasswordGenerator();

    System.out.println(pg.uppercaseLetters());

    }

}
m.aibin
  • 3,528
  • 4
  • 28
  • 47
James B
  • 221
  • 3
  • 14
  • 1
    Duplicate of http://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java – George Mulligan Jan 15 '16 at 04:17
  • Possible duplicate of [How to concatenate characters in java?](https://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java) – cellepo Nov 21 '18 at 01:50

4 Answers4

3

One of String's constructors takes a char[], so that lets you construct the String straightforwardly, without concatenation tricks or StringBuilder.

result = new String(new char[] { char1, char2 });
yshavit
  • 42,327
  • 7
  • 87
  • 124
2

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

result = String.valueOf(char1) + char2;

or something like

result = "" + char1 + char2;

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.

class PasswordGenerator {
    final Random rand = new Random();
    final static String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public String uppercaseLetters(int len) {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            sb.append(upperCase.charAt(rand.nextInt(upperCase.length())));
        }
        return sb.toString();
    }
}

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you, could you please explain what the double quotes are for? Why does the code not work without them? – James B Jan 15 '16 at 04:20
  • 1
    @JamesB The **empty string** (`""`) makes it `String + char + char` which is `String`, `char + char` is an `int` (because `char` is an integral type, and `'a' + 1 == 'b'`) – Elliott Frisch Jan 15 '16 at 04:27
  • The superscript 1 tag does not explain the 1st sentence of this Answer that is tagged with that. Please edit to clarify that confusing disconnect. – cellepo Nov 21 '18 at 01:47
1

You can use StringBuilder,

public String uppercaseLetters() {
        char char1;
        char char2;
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String result;

        char1 = alphabet.charAt(rand.nextInt(25));
        char2 = alphabet.charAt(rand.nextInt(25));

        result = new StringBuilder().append(char1).append(char2).toString();

        return result;
    }

or just return new StringBuilder().append(char1).append(char2).toString();

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Change one line:

result = "" + char1 + char2;

Also, you don't need to create variable result, you can simply do:

return = "" + char1 + char2;
m.aibin
  • 3,528
  • 4
  • 28
  • 47