The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Java casting/combining char

I'm trying to make the program generate a digit from 97-122, then convert the integer into a char then combine all the char together, but I have no idea how to combine all the char together. My code is

So the LowerLAmount is the user input of how many char there should be, for example, if the user input "8" the loop would run 8 time and generate 8 random number from 97-122 ( hence the Math.random), then I want to turn the integer that was generated into char. For example let say the user input "2", the loop when generate 2 number from 97-122. Let say it generate 97 and 98. I would then want to convert the 97 and 98 into char, which would be "a" and "b". Next I want to combine the two together so it print out "ab". Thanks

Re: Java casting/combining char

Oh sorry, I wanted to explain it throughly and say where I'm stuck at, so I wrote that long paragraph at the bottom lol. But I'm stuck at trying to combine "char" together. Kind of hard to explain without saying what I wrote in that last paragraph again. Sorry

Re: Java casting/combining char

Ah, I see. You can create a String *before* your loop, and then inside of the loop, concatenate the char onto the String using the += operator.

Note that a StringBuilder will do this more efficiently than a String, but your purposes, a String will work adequately.

Note that you are using "magic" numbers, 25, 97, 122, and this can create code that is hard to debug. You should try to avoid doing this and instead use variables that make sense. Here 'a' represents 97 and 'z' 122, and so the code would make more sense using these chars: