reset password
Author Message
dliang
Posts: 35
Posted 22:44 Mar 22, 2019 |

Does anyone have any idea, how to print out the output twice? For what I did in the Names Generator 1, which I isolated the first uppercase letter and used the for loop for the lowercase letter. It works great, but my output can only appear once. In NameGenerator1A, the requirement asking for the "name" must appear in the output at least twice.

wcwir
Posts: 75
Posted 19:15 Mar 24, 2019 |

I just realized I put my reply to this in a different thread, so here it is again:

If you want to print out a randomly generated sequences of characters twice, you need to save it as you generate it. How do you save a sequence of characters? Java has a data type for this: String.

I just looked through the textbook, and this isn't covered very well -- but basically you can take separate characters and build a String out of them.

You have to be careful though:

Try this: 

String s = 'a' + 'b';

and it won't work -- that is because Java will think that you are using the the two char literals 'a' and 'b' as their numerical equivalents, and it will think you are trying to add them.

If you want to concatenate them instead, add them to an empty String:

String s = '"" + 'a' + 'b';

That will work, and now you have the String "ab" which you can reuse as you please.