*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with an application" Watch "Need help with an application" New topic
Author

Need help with an application

Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Hello,
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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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

Reason:
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
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Joel,
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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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
SCJP, MCDBA
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.
Layne
[ April 29, 2003: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
 
wood burning stoves
 
subject: Need help with an application
 
Similar Threads
Someone up for a challenge?
Caesar cipher
Ecryption program question
Need some help with output to file
Need help extending a program.