File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need help with an application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with an application" Watch "Need help with an application" New topic

Need help with an application

Jere Johnson

Joined: Mar 29, 2003
Posts: 28
This java application will fill an array with randomly chosen uppercase letters of the alphabet. Program will write letters
to a file and display them. I cant seem to get the characters to show on the black output screen. I get weird little icons instead of characters. Am i doing something wrong?

Diapers are the best invention
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Your line of code:
character = (char)(Math.random()*26);
should read:
character = (char)(Math.random()*26) + 65;

ASCII capital characters start with 65 ('A' = 65, not 'A' = 0) What you were displaying on your screen were mostly NPCs --> 0 through CTRL-Y

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jere Johnson

Joined: Mar 29, 2003
Posts: 28
I get an possible loss of precision error
when i put + 65 at the end of the statement
like this:
character = (char)(Math.random()*26)+65;
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Oops. Try:
character = (char)(Math.random()*26 + 65);
You have to cast the whole expression to a char, not just the Math.random()*26.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Jeremiah
According to Joel given

It may be better.

Francis Siu
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You also may wish to use char literals to make your intent more clear:

In fact, you can also generalize this to create a handy helper method:

The implementation is left as the proverbial exercise for the reader. Such a function could be easily used for generating a random char as well. Or you can write an overloaded version for char.
[ April 29, 2003: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Need help with an application
It's not a secret anymore!